MATLAB Answers

Able to run GUI from guide command interface, but not when opening .fig straight in folder

33 views (last 30 days)
Quy
Quy on 30 Sep 2011
Commented: Walter Roberson on 19 Sep 2018
after typing in "guide" in the command window and then opening an existing GUI, and then using the GUI editor and pressing "Run Figure (Ctr+T)," everything works fine (no error). However, when opening the GUI directly by double clicking the .fig file in the windows explorer, and attempting to execute by pressing on my pushbuttons I get the following error in the command window:
??? Attempt to reference field of non-structure array.
Error in ==> GUI_CompSurf>ParaRest_dir_browse_Callback at 154
set(handles.Data_dir_label,'String',parameter_restrictions)
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> GUI_CompSurf at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)GUI_CompSurf('ParaRest_dir_browse_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
When debugging, the error seems to be at a set() command:
data_directory = uigetdir(pwd,'Select Data Directory, where your datas are located');
set(handles.Data_dir_label,'String',data_directory) %<---error here
"Data_dir_label" is static text that show the directory string, at least it should.
If anyone has any idea, it would be most appreciated.
Thank you.

  4 Comments

Show 1 older comment

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 30 Sep 2011
I do not know if opening the figure by double clicking on it would invoke the Figure Open Callback, but surely opening the figure under those circumstances would invoke the Figure Resize Callback. So you can stuff code in there that detects whether the GUI has been initialized properly, and initializes it if not.
There are some tricky spots to doing this, but it should be possible in theory.

  2 Comments

Jan
Jan on 10 May 2015
Alternatively the WindowButtonMotionFcn is called also, when the user moves the mouse into the figure.
Jake G
Jake G on 27 May 2016
Thank you for the help! However...I have a close request function so if I accidentally open it by double clicking the fig file when I meant to open it in GUIDE I have to close all of Matlab to get rid of the windows :(

Sign in to comment.

More Answers (5)

Quy
Quy on 30 Sep 2011
from one of the engineer that I work with: the .fig file is just an shell. it does not contain all the initialization that the .m file (with the same name as the .fig file) has. Therefore it will not work."
If you actually open up the .m file that has the same name as the .fig file, and run it from there, everything will work fine.
Now a different question: How do you make everything works just fine by just double clicking the .fig file? How do you "initialize" the GUI without having to open the .m file everytime? Is this possible?

  0 Comments

Sign in to comment.


Fangjun Jiang
Fangjun Jiang on 30 Sep 2011
I can repeat what you did and got the same error. I didn't a quick debug and it seems that the GUI is not properly initialized. Then I did a search and found the following link. See the last paragraph. It is suggest ed not to double-click the .fig file to open the GUI.
If you really like to have this "double-click" to run the application capability, I can think of:
  1. Compile the .m file to .p file and double-click the .p file
  2. Change the settings of MATLAB so you can double-click the .m file to run the .m file, instead of edit the .m file. I remember there is a QA on this forum for that.
  3. Compile your GUI application to an .exe file. You'll need to have the MATLAB Compiler Toolbox.

Vlad Atanasiu
Vlad Atanasiu on 10 May 2015
You could use a double-click on the thefig.fig file to run your GUI if you include in it a call to the function thefig_OpeningFcn of the thefig.m file. This way the GUI is reading all the setup information it needs, i.e. from both .fig and .m files.
Note that you need to leave the GUI sufficient time to update before being able to use it. The call to thefig_OpeningFcn might be hidden in a button that doesn't need data defined in thefig_OpeningFcn. In that case, to get the GUI running, you double click on thefig.fig, followed by a click on the special button.

  2 Comments

Jan
Jan on 10 May 2015
Instead of using the manually triggered callback of a button, The ResizeFcn as suggested by Walter is called automatically when the fig-file is opened by a double click. The WindowButtonMotionFcn might be useful also, because it can be assumed that it is called before any other interaction by the user.
Walter Roberson
Walter Roberson on 19 Sep 2018
thefig_OpeningFcn is not called if you open the fig file directly. The OpeningFcn is called by the prolog code that GUIDE inserts that is only invoked if the .m file is run.

Sign in to comment.


Harris Yong
Harris Yong on 15 Sep 2016
Is there a way to disable the GUI from even opening when double-clicking on the .fig? That'd force people to open it some other way.

  2 Comments

Image Analyst
Image Analyst on 16 Sep 2016
Before the line where it says
% Begin initialization code - DO NOT EDIT
put this code:
if nargin ~= 0
close gcf;
return;
end
% Begin initialization code - DO NOT EDIT
You can also craft an error message and display it if you want, before the return:
errorMessage = sprintf('You cannot.......
uiwait(errordlg(errorMessage));
return;

Sign in to comment.


MUKHTAR MANSURI
MUKHTAR MANSURI on 17 Sep 2018
My gui is working properly when using with matlab and even after compilation in my laptop.. But when I install this executive file in others laptop it is showing error (error in line 42, which is main function line (not editable) in my matlab code ). I have compiled many times my code, but it is showing the same error in others laptop. Help to install my code for public domain.

Community Treasure Hunt

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

Start Hunting!