MATLAB: Running GUI via double click on a .fig file makes handles be empty

empty handlesgui

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')

Best Answer

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.
Related Question