Why running GUI via double click on a .fig file makes handles be empty?

Hi all,
I made some primitive GUI program with push button ant static text. When the button has pressed, in a window of static text will appear word OK. When I run its .m file or via a button "Run figure" it work, but when run it by double click on the .fig file, I get error becouse the handles is empty.
What I can do to solve the issue?
Below I give the code of TestText.m file and attach the TestText.fig file.
function varargout = TestText(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @TestText_OpeningFcn, ...
'gui_OutputFcn', @TestText_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
function TestText_OpeningFcn(hObject, eventdata, handles, varargin)
guidata(hObject, handles);
function varargout = TestText_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles;
function pushbutton1_Callback(hObject, eventdata, handles)
set(handles.text2,'String','OK')

 Accepted Answer

Rik
Rik on 23 Dec 2018
Edited: Rik on 23 Dec 2018
The fig file is just a figure, and does not contain any code. So opening the figure does not start your GUI, it only opens that figure, which contains some objects. Especially for simple GUIs I would suggest avoiding GUIDE. I use GUIDE to get a broad idea about how my GUI should look, then I make the GUI with normal code. The advantage is that my code works for many releases and can easily be adapted if needed. GUIDE adds a lot of bloat code and the auto-generated help texts isn't even correct anymore.
My small guide to avoid GUIDE:
  • Make a figure (with f=figure;) and look into the doc for figure which properties you want to turn off (you probably want to set Menu and Toolbar to 'none')
  • Create buttons and axes and everything you need with functions like uicontrol and axes. Save the handles to each element to fields of a struct (like handles.mybutton=uicontrol(___);)
  • When you've finished loading all data (and saving it to fields of your handles struct), and creating all the buttons, save your handles struct to the guidata of your figure like this guidata(handles.f,handles);. (You can also use getappdata and setappdata)
  • You can set the Callback property of many objects. If you do, use a function name with an @ in front, or a char array that can be evaluated to valid code. (like @MyFunction or 'disp(''you pushed the button'')')
  • Callback functions will be called with two arguments: the first is a handle to the callback object, the second is eventdata that may contain special information. To get access to your data, just use handles=guidata(gcbo);. You can replace the gcbo function with the name of the first input to your callback function if you prefer.
  • More information about callbacks can be found in multiple places in the doc, for example here.

3 Comments

Hi Rik, Thanks for answer.
I already get, that the best way to start work with GUI, it just type in a command line the name of a file without extension .fig. But in Matlab's version I used in 2004 it should be enough to make double cleak on a .fig file. Thanks for the guide.
Double-clicking on a .fig file has never been enough to run a full GUI. .fig files have always been renamed .mat files that have particular variable names that MATLAB recognizes that are struct() variables, a field of which contains a handle to a saved figure which is loaded. This does not load or execute the code corresponding to the saved figure.
There is one thing you can do if you are building everything by hand: you can assign a CreateFcn callback to the figure itself before you savefig(). CreateFcn callback are called after the object is created, and that happens even for objects that are loaded from a .fig . CreateFcn can call whatever code is necessary.
Note that if you use the handles structure, that the handles structure is not created by MATLAB itself: it is created by the code that is normally invoked when you run the function that has the same name as the .fig file. You would either need to make that call yourself, or do something equivalent, or else not use a handles structure.
It is not documented whether CreateFcn callbacks are called immediately after the object is created, or not until after sub-objects that are children of the object are created, or what the relative timing is for creating objects attached to UserData or application data (setappdata) on an object.
Thanks Walte,
I remembered, how I run my gui, avoiding typing a file name in a command line.Just, after right click on a appropriated .m file, I choose "Run". It possibly, cause to the handles be not empty. Sometimes the solution is much simpler than I expected.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 23 Dec 2018

Edited:

on 25 Dec 2018

Community Treasure Hunt

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

Start Hunting!