Why are my GUI handles disappearing?

5 views (last 30 days)
Lyn
Lyn on 17 Jan 2015
Edited: Image Analyst on 20 Jan 2015
Hi,
I have a "handles.anglenames" that I write into my guide GUI's opening function.
When I run the code however, I get the following error:
"Attempt to reference field of non-structure array.
Error in ZenKinematicsTest>graphpop1_CreateFcn (line 133)
set(hObject,'String',handles.anglenames);"
and upon debugging, I realise that at some point in my code, handles suddenly becomes empty. Inside the opening function it is as it should be (i.e. contains the handles to all my gui objects, and also has handles.anglenames) ... but once it enters any of the createFcns, suddenly handles = []
The code is below, and I've attached the m file and fig as well. No idea what's going on I'm afraid..! :( I'm using Matlab 2014b.
function varargout = ZenKinematics(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ZenKinematics_OpeningFcn, ...
'gui_OutputFcn', @ZenKinematics_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
% End initialization code - DO NOT EDIT
% --- Executes just before ZenKinematics is made visible.
function ZenKinematics_OpeningFcn(hObject, eventdata, handles, varargin)
handles = guidata(hObject);
handles.anglenames = {'Knee','Ankle','Hip'};
uiwait(hObject);
guidata(hObject,handles);
% --- Outputs from this function are returned to the command line.
function varargout = ZenKinematics_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%handles = guidata(hObject);
%handles.ref = getappdata(0,'handlesL');
%set(handles.popupmenu1, 'String', handles.ref.dancernames);
%guidata(hObject, handles)
% --- Executes on selection change in graphpop1.
function graphpop1_Callback(hObject, eventdata, handles)
% hObject handle to graphpop1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns graphpop1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from graphpop1
handles = guidata(hObject);
AngleID = find(strcmp(handles.anglenames,contents));
disp(AngleID)
% --- Executes during object creation, after setting all properties.
function graphpop1_CreateFcn(hObject, eventdata, handles)
% hObject handle to graphpop1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
set(hObject,'String',handles.anglenames);
% --- Executes during object creation, after setting all properties.
function graph1_CreateFcn(hObject, eventdata, handles)
% hObject handle to graph1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

Accepted Answer

Geoff Hayes
Geoff Hayes on 17 Jan 2015
Or is it the order of operations? Does the _OpeningFcn get called before the _CreateFcn (for each control), or is it the other way around?
I think that the create functions get called before the opening function, and so the handles structure will be empty at that point. Note the general comment for the OpeningFcn
% --- Executes just before untitled is made visible.
In fact, if you put breakpoints in the create functions and the opening function, then you should observe that the latter get called before the former.
Since you just want to update your controls with data that is initialized in the opening function, then just do so from there as
% --- Executes just before ZenKinematics is made visible.
function ZenKinematics_OpeningFcn(hObject, eventdata, handles, varargin)
handles = guidata(hObject);
% update with the angle names
handles.anglenames = {'Knee','Ankle','Hip'};
% save the changes to the handles structure
guidata(hObject,handles);
% update the controls
set(handles.graphpop1,'String',handles.anglenames);
% uiwait(hObject);
And just remove the equivalent set from the graphpop1 create function and you should be good to go.
I commented out the uiwait in your above code as I don't think that it was in the correct place (before the call to guidata) and am not sure what it's intent is.
  1 Comment
Lyn
Lyn on 20 Jan 2015
Ah yes.. I was writing it into the create fcn, not realizing it said right there in the comments "% handles empty - handles not created until after all CreateFcns called" so my handles was emptying out any time I tried to call/change them inside the create fcns. :(
Works now with everything in opening fcn! thank you!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 17 Jan 2015
Edited: Image Analyst on 20 Jan 2015
I didn't wade through the code but there are three common reasons/mistakes.
  1. One is calling "clear" somewhere in your callback that blows away the handles variable.
  2. The other is that you attached that field in some function but you did not return handles or call guidata, so that field you attached is essentially a local variable and once you leave that function, handles returns to it's original state since all local variables are lost once a function exits.
  3. You're doing something with the handles structure within a CreateFcn function. The handles structure will not exist until all of the CreateFcn functions have been called and the GUI is made visible (like the comment in those types of functions says).

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!