Why are my GUI handles disappearing?
Show older comments
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
More Answers (1)
Image Analyst
on 17 Jan 2015
Edited: Image Analyst
on 20 Jan 2015
3 votes
I didn't wade through the code but there are three common reasons/mistakes.
- One is calling "clear" somewhere in your callback that blows away the handles variable.
- 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.
- 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 Simulink 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!