Able to run GUI from guide command interface, but not when opening .fig straight in folder
조회 수: 4 (최근 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.
채택된 답변
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
2015년 5월 10일
Alternatively the WindowButtonMotionFcn is called also, when the user moves the mouse into the figure.
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개)
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:
- Compile the .m file to .p file and double-click the .p file
- 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.
- Compile your GUI application to an .exe file. You'll need to have the MATLAB Compiler Toolbox.
댓글 수: 2
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
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
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
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
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;
Walter Roberson
2018년 9월 19일
I would tend to think that should be "nargin == 0" rather than "nargin ~= 0"
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.
댓글 수: 1
Walter Roberson
2018년 9월 19일
(You created https://www.mathworks.com/matlabcentral/answers/419319-error-error-in-line-42-in-using-matlab-execution-guide-compiled-file for this question)
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!