Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
handles 'non-structure' error when using GUI

Subject: handles 'non-structure' error when using GUI

From: Brenton Graefe

Date: 13 Jan, 2011 04:49:05

Message: 1 of 10

I'm having a problem in my GUI design trying to use the handles structure to get information about other parts of the GUI. I designed my GUI in GUIDE and then generated my m-file. I have a callback function for a push button that is supposed to find out which of several radio buttons on a button panel is selected. I tried getting this information using

 h=get(handles.devicePanel,'SelectedObject');

Everytime I run the code, it gives me the error:

??? Attempt to reference field of non-structure array.

Error in ==> deviceTest1>testButton_Callback at 92
h=get(handles.devicePanel,'SelectedObject');

I dug a little deeper and did some debugging and found that the handles structure that is being used in my function is actually empty, and therefore unusable. I'd really like to figure out why handles is seemingly deleted or not being passed correctly, but I can't figure out why that is. I've posted all my code so errors in some other part can be checked for, but since it is exactly what GUIDE generated, except for my one line, I'm really at a loss for what this error could be. Any help with this would be GREATLY appreciated, I've worked on it for awhile now and can't figure out what's going on.

This is the entirety of my m-file:

function varargout = deviceTest1(varargin)
% DEVICETEST1 M-file for deviceTest1.fig
% DEVICETEST1, by itself, creates a new DEVICETEST1 or raises the existing
% singleton*.
%
% H = DEVICETEST1 returns the handle to a new DEVICETEST1 or the handle to
% the existing singleton*.
%
% DEVICETEST1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DEVICETEST1.M with the given input arguments.
%
% DEVICETEST1('Property','Value',...) creates a new DEVICETEST1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before deviceTest1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to deviceTest1_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help deviceTest1

% Last Modified by GUIDE v2.5 25-Oct-2010 15:41:34

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
                   'gui_Singleton', gui_Singleton, ...
                   'gui_OpeningFcn', @deviceTest1_OpeningFcn, ...
                   'gui_OutputFcn', @deviceTest1_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 deviceTest1 is made visible.
function deviceTest1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to deviceTest1 (see VARARGIN)

handles=guidata(hObject);
% Choose default command line output for deviceTest1
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

%set(handles.devicePanel,'SelectionChangeFcn',@devicePanel_SelectionChangeFcn);

% UIWAIT makes deviceTest1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = deviceTest1_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 button press in testButton.
function testButton_Callback(hObject, eventdata, handles)
% hObject handle to testButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)


h=get(handles.devicePanel,'SelectedObject');

function outputText_Callback(hObject, eventdata, handles)
% hObject handle to outputText (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of outputText as text
% str2double(get(hObject,'String')) returns contents of outputText
% as a double


% --- Executes during object creation, after setting all properties.
function outputText_CreateFcn(hObject, eventdata, handles)
% hObject handle to outputText (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit 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

Subject: handles 'non-structure' error when using GUI

From: Steven_Lord

Date: 13 Jan, 2011 14:58:06

Message: 2 of 10



"Brenton Graefe" <bgraefe@gmail.com> wrote in message
news:igm081$rqb$1@fred.mathworks.com...
> I'm having a problem in my GUI design trying to use the handles structure
> to get information about other parts of the GUI. I designed my GUI in
> GUIDE and then generated my m-file. I have a callback function for a push
> button that is supposed to find out which of several radio buttons on a
> button panel is selected. I tried getting this information using
>
> h=get(handles.devicePanel,'SelectedObject');
>
> Everytime I run the code, it gives me the error:
>
> ??? Attempt to reference field of non-structure array.
>
> Error in ==> deviceTest1>testButton_Callback at 92
> h=get(handles.devicePanel,'SelectedObject');
>
> I dug a little deeper and did some debugging and found that the handles
> structure that is being used in my function is actually empty, and
> therefore unusable. I'd really like to figure out why handles is seemingly
> deleted or not being passed correctly, but I can't figure out why that is.
> I've posted all my code so errors in some other part can be checked for,
> but since it is exactly what GUIDE generated, except for my one line, I'm
> really at a loss for what this error could be. Any help with this would be
> GREATLY appreciated, I've worked on it for awhile now and can't figure out
> what's going on.

How are you launching the GUI? Are you running the function file (the
correct way) or are you just opening the figure file (the wrong way)?

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: handles 'non-structure' error when using GUI

From: Ralph

Date: 9 May, 2013 21:18:09

Message: 3 of 10

> How are you launching the GUI? Are you running the function file (the
> correct way) or are you just opening the figure file (the wrong way)?

What exactly constitutes this being "the right way"? Is this communicated to the user at any time? In any of the MathWorks tutorials?

The answer is 'No'. As with most things MathWorks related, the user is simply expected to know and then mocked for not knowing. Poor showing MathWorks.

Why does double-clicking the .fig file give the appearance of normal behavior but not properly initialize the GUI? Who thought that was a good idea? Why is that person still with the company? Why haven't their mistakes been fixed?

Subject: handles 'non-structure' error when using GUI

From: Steven_Lord

Date: 10 May, 2013 14:55:11

Message: 4 of 10



"Ralph " <ralph.e.nothdurft@monsanto.com> wrote in message
news:kmh3qh$ina$1@newscl01ah.mathworks.com...
>> How are you launching the GUI? Are you running the function file (the
>> correct way) or are you just opening the figure file (the wrong way)?
>
> What exactly constitutes this being "the right way"? Is this communicated
> to the user at any time? In any of the MathWorks tutorials?

The documentation indicates that running the function file is the right way,
yes, and it depends on what you consider "the MathWorks tutorials" to be.

http://www.mathworks.com/help/matlab/creating_guis/running-a-guide-gui.html

See the first Note box.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: handles 'non-structure' error when using GUI

From: Céldor

Date: 11 May, 2013 02:45:10

Message: 5 of 10

> guidata(hObject, handles);

Hi Brenton

If you use GUIDE, I don't know if you can mess up with guidata(). Although it's one of the three methods to pass / receive data from GUI objects, GUIDE uses guidata() for its own purpose, and this purpose is if I am not mistaken to keep all GUI handles. That means, the third parameter of each callback function has parameter 'handles' that stores all handles in a structure. I did not test it and I am not sure if this causes your problem but I would think about it.

As a side note, there's a better way to store your own data/handles etc. By using setappdata(ObjectHandle, 'yourIdData', variable/structure/etc.), you can store anything. You can get it back by using getappdata(ObjectHandle, 'yourIdData'). ObjectHandle can be any handle (even 0 that means MATLAB root/global workspace) and usually it is the main Figure handle. Additionally, you can test if an object has already something stored by using isappdata(ObjectHandle,'yourIdData') or remove data when executing rmappdata(ObjectHandle,'yourIdData'). 'yourIdData' can be any valid name. Also, I think you can store more than one data in the same ObjectHandle when using other id names.

I hope it helps, although I could not test anything what I described.

Regards

Subject: handles 'non-structure' error when using GUI

From: Nasser M. Abbasi

Date: 11 May, 2013 03:40:54

Message: 6 of 10

On 5/10/2013 9:45 PM, Céldor wrote:
>> guidata(hObject, handles);
>
> Hi Brenton
>
> If you use GUIDE, I don't know if you can mess up with guidata().

You, ofcourse you can use guidata() when using guide also.

--Nasser

Subject: handles 'non-structure' error when using GUI

From: Céldor

Date: 11 May, 2013 20:32:07

Message: 7 of 10

"Nasser M. Abbasi" wrote in message <kmkek8$c0u$1@speranza.aioe.org>...
> On 5/10/2013 9:45 PM, Céldor wrote:
> >> guidata(hObject, handles);
> >
> > Hi Brenton
> >
> > If you use GUIDE, I don't know if you can mess up with guidata().
>
> You, ofcourse you can use guidata() when using guide also.
>
> --Nasser

GUIDE uses GUI Data to store all handles related to a GUI. Documentary does not say it could not be used. I could have been more precise and said anybody could read the structure containing handles and added / removed any field to it but in my opinion it would be easy to mess up / overwrite something that would be important in order for GUIDE to work. I would not use it by myself and that is why I recommended different mechanism to OP.

In my opinion, it is similar to an issue with global variables. You still can write functional scripts / functions that use global variables but would you do it?

Subject: handles 'non-structure' error when using GUI

From: Bruno Luong

Date: 12 May, 2013 07:19:07

Message: 8 of 10

I copy my post from an old tread
http://www.mathworks.fr/matlabcentral/newsreader/view_thread/296722:

There are at least five ways of passing data among Callbacks.

1) guidata
2) setappdata/getappdata
3) using the field 'userdata' of graphic handles
4) using nested functions
5) using global variables

I prefer them by order they are listed.

Bruno

Subject: handles 'non-structure' error when using GUI

From: Céldor

Date: 14 May, 2013 12:53:09

Message: 9 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kmnfpb$spj$1@newscl01ah.mathworks.com>...
> I copy my post from an old tread
> http://www.mathworks.fr/matlabcentral/newsreader/view_thread/296722:
>
> There are at least five ways of passing data among Callbacks.
>
> 1) guidata
> 2) setappdata/getappdata
> 3) using the field 'userdata' of graphic handles
> 4) using nested functions
> 5) using global variables
>
> I prefer them by order they are listed.
>
> Bruno

Using the first option when GUI is designed in GUIDE is prone to errors simply because one needs to share the same structure with GUIDE that is passed between callbacks . Fourth option is also difficult to realise with GUIDE because the main GUI function is created in such a way that callbacks are placed outside the main function.
As such, GUIDE users are limited in terms of aforementioned mechanisms.

Subject: handles 'non-structure' error when using GUI

From: Steven_Lord

Date: 14 May, 2013 13:29:03

Message: 10 of 10



"Cldor " <zebik@op.pl> wrote in message
news:kmtc3l$9fm$1@newscl01ah.mathworks.com...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> <kmnfpb$spj$1@newscl01ah.mathworks.com>...
>> I copy my post from an old tread
>> http://www.mathworks.fr/matlabcentral/newsreader/view_thread/296722:
>>
>> There are at least five ways of passing data among Callbacks.
>>
>> 1) guidata
>> 2) setappdata/getappdata
>> 3) using the field 'userdata' of graphic handles
>> 4) using nested functions
>> 5) using global variables
>>
>> I prefer them by order they are listed.
>>
>> Bruno
>
> Using the first option when GUI is designed in GUIDE is prone to errors
> simply because one needs to share the same structure with GUIDE that is
> passed between callbacks . Fourth option is also difficult to realise with
> GUIDE because the main GUI function is created in such a way that
> callbacks are placed outside the main function.
> As such, GUIDE users are limited in terms of aforementioned mechanisms.

While the name of the struct array in which GUIDE stores the handles of the
objects that make up the GUI is "handles", that struct array is not limited
to containing just object handles. As long as you don't overwrite the fields
that contain the object handles, you can store whatever you want in there
and update the "master copy" using GUIDATA. Choose your field names
carefully. See the blue box on the reference page for GUIDATA as well as the
second example on that page.

http://www.mathworks.com/help/matlab/ref/guidata.html

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us