Why do I get errors when simply opening a .fig file generated by GUIDE?
4 次查看(过去 30 天)
显示 更早的评论
I'm trying to make a GUI in Matlab for the first time. I'm following the tutorial on the Mathworks website, and the GUIDE editor creates a .m file as well as a .fig file. I get that the .fig file by itself does not do anything, that you need the .m file to run the GUI.
However, I don't understand what the issue is to just double click and open the .fig file as I would with any other .fig file in Matlab. When I do that, it does open and display the interface, but I also get several lines of errors in the command window:
Undefined function 'untitled' for input arguments of type
'char'.
Error in
@(hObject,eventdata)untitled('popupmenu1_CreateFcn',hObject,eventdata,guidata(hObject))
Error using struct2handle
Error while evaluating uicontrol CreateFcn
Why is this happening? What's so special about this .fig file compared to other .fig files? What is Matlab doing behind the scenes that is causing it to freak out?
**************
ALSO: I just tried closing Matlab completely, and opening it again, and now it's no longer giving me any errors! Any ideas what's happening?
My Matlab version is 2011b.
0 个评论
回答(1 个)
Orion
2014-11-7
Alexei,
When you create a GUI with GUIDE, you always have two files generated
untitled.m % script
untitled.fig % figure object
the way you open a gui generated by guide is by running the mfile. this mfile will open the fig file and initialize it. and then you can play with it.
if you open directly the .fig, you get the figure and the visible buttons, but most of the time it will crash because your interface is not initialized (it depends of what you code).
the initialization is usually happening in the OpeningFcn of the generated mfile.
% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
see in the generated code % --- Executes just before untitled is made visible.
it does mean that this portion of code is executed before the figure is made visible
and the line
guidata(hObject, handles);
is the one which updates all your buttons (and their callbacks,and...).
And so, in the openingFcn, you can add any code you might want to be executed when opening the gui.
but if are a beginner, try to not mess with the code, especially the first part
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
do not modify it, until you don't fully understand the way it works.
3 个评论
Orion
2014-11-7
because a .fig is not an image, it's a matlab object.
you can't open it with an other software.
when you save a figure as .fig, you actually save all the graphic objects in this figure (parent and children). The interest is that you can work with this object later (in another session). Just try it : make a simple plot, save as .fig, then open the .fig, you will be able to zoom, pan and do all other graphical actions. you can't do that with just an "image".
but when it's just plots, surf, or any other graphical treatment, the data created are static. In a gui, and that's the difference, you put some buttons which have callbacks. these callbcak must be initialized.
that's the error you posted.
you have inserted a popupmenu in your gui, and the createFcn is executed when the object is created (meaning when the figure is created). but when you open directly the .fig, matlab doesn't find the associated function, and so you get the error. If you run the mfile, the button is initialized within the code, and so it works.
per isakson
2014-11-7
编辑:per isakson
2014-11-7
"The point is that the .fig file is just a picture"   That's not quite true. Trust the documentation and the answer by Orion.
"I get that the .fig file by itself does not do anything, that you need the .m file to run the GUI."   Neither this is true. It is a semantic question whether the fig-file does anything by itself. However, MATLAB obviously fires callbacks stored in the fig-file when you "open" the figure.
另请参阅
类别
在 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!