Compiling a Matlab GUI to standalone application - invalid file error

4 visualizzazioni (ultimi 30 giorni)
Hi,
I've written a fairly elaborate GUI file in Matlab using GUIDE that includes several other linked GUI files and text configuration files. The program works well when operated inside Matlab as a .m file. However, when I try to compile into a Standalone application, I get the following error:
*Invalid file identifier: Use fopen to generate a valid file identifier. Error in => collectCCDSpectrum.m at line 45.*
When I go to line 45 in my code, it points to a line in the constructor function for the GUI
gui_mainfcn(gui_State, varargin{:});
I've successfully compiled other GUIs with my computer. I'm only having a problem with this one. I tried to include all of the dependent files. I'm using Matlab R2015a with Windows SDK 7.1.
Thanks, Kevin
  1 Commento
Luke Howell
Luke Howell il 21 Ago 2017
Hi,
Did this ever get solved? I have the same problem, the code works fine on one machine but not another. The code it points to is the automatically generated 'initialization code' for the whole GUI, which is noted as 'do not edit', so there is no 'fopen'/ 'fclose' stuff going on there.
Let me know,
Luke

Accedi per commentare.

Risposte (2)

Walter Roberson
Walter Roberson il 2 Set 2015
You have code of the form
fid = fopen(SomeFile);
data = fread(fid, ...);
or otherwise are attempting to read from the file. But you are not checking first that your fopen() was successful by checking if fid < 0
You should always check the result of fopen(), using a form such as
[fid, message] = fopen(SomeFile, KindOfAccessRequired);
if fid < 0
display message as it tells you why the open failed
return
end
If you gave an absolute pathname then the file is not there (or you do not have permissions). If you did not give an absolute pathname then you failed to "attach" the file. When you fopen() a file for reading in a deployed application, the file will first be looked for in the attached files before the MATLAB path is looked through.
Doing the test of whether the fopen worked will allow you to tell the user what failed, which file was being looked for. And you could even uigetfile() to allow the user to find the file to try again.
  2 Commenti
Luke Howell
Luke Howell il 21 Ago 2017
Wlater, I don't believe he has code of the form:
fid = fopen(SomeFile);
data = fread(fid, ...);
I have the same problem. in the 'initialization code' for a GUI that is automatically generated and cannot be edited, and does not contain the above code. It may be called in a matlab function, potentially gui_mainfcn.
Walter Roberson
Walter Roberson il 21 Ago 2017
I am absolutely certain that I was correct in my original statement about the pattern of code. The code is not necessarily in a file that Kevin wrote: in Kevin's case, it was inside collectCCDSpectrum.m which might have been supplied by someone else.
The error gets noted under gui_mainfcn because the code where the problem occurs does not protect itself with a try/catch block or by testing that the fopen() was successful, leading to an error being generated; the function with the problem is being called by something invoked by gui_mainfcn .

Accedi per commentare.


Jacob Kinnun
Jacob Kinnun il 24 Nov 2018
For those who are still stuck on this! If you used "pwd" anywhere in your code to get a location of a file it can give you an error if a user attempts to access your compiled program through the shortcut. Even if its later in the code it will appear the first time "gui_mainfcn(gui_State, varargin{:});" occurs since guide as it embedded in a function. In my code (Seen Here) I originally had.
handles.program_directory = pwd; % Main directory
Which returns the directory of wherever the user opened a shortcut from, I found from:
I need to do this to actually get the deployed application's directory when deployed:
% get program directory windows
if isdeployed % Stand-alone mode.
[~, result] = system('path');
handles.program_directory = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
else % MATLAB mode.
handles.program_directory = pwd;
end
And this fixed my file not found errors! In the link I think there is an option for Mac versions as well.

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by