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

7 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2011-9-30
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 个评论
Jan
Jan 2015-5-10
Alternatively the WindowButtonMotionFcn is called also, when the user moves the mouse into the figure.
Jake G
Jake G 2016-5-27
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 :(

请先登录,再进行评论。

更多回答(5 个)

Quy
Quy 2011-9-30
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?

Fangjun Jiang
Fangjun Jiang 2011-9-30
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 2015-5-10
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 个评论
Jan
Jan 2015-5-10
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 2018-9-19
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.

请先登录,再进行评论。


Harris Yong
Harris Yong 2016-9-15
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 个评论
Image Analyst
Image Analyst 2016-9-16
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;

请先登录,再进行评论。


MUKHTAR MANSURI
MUKHTAR MANSURI 2018-9-17
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.

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by