MATLAB: Are the GUI handles disappearing

guiguidehandles

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

Best Answer

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