MATLAB Answers

GUI error "Attempt to reference field of non-structure array. " at start

15 views (last 30 days)
Felix on 10 Dec 2012
(I found almost the same problem in this question:, but it was not resolved, so I start a new post here)
I defined "handles.xy" in the OpeningFcn of my gui with:
set(handles.xy, 'string', 'bla'),
but when I try to run the gui I get the error message "Attempt to reference field of non-structure array." because in a later function I'm trying to access this handle:
get(handles.xy, 'string')
and in the debugging view handles.xy is empty. Why is it empty, is the OpeningFcn not executed by default or something??

Answers (3)

Jan on 10 Dec 2012
Edited: Jan on 10 Dec 2012
This line does not define "handles.xy":
set(handles.xy, 'string', 'bla')
It assignes the string 'bla' to the string property of the GUI object the handle handles.xy point to. It could be (run this from the command line):
handles.FigH = figure;
handles.ButtonH = uicontrol;
set(handles.ButtonH, 'String', 'Hello!');
To set the value of a field:
handles.xy = 'bla';
Such details are explained exhaustively in the "Getting Started" chapters of the documentation. It is not possible to use such a powerful language as Matlab without reading the instructions.
[EDITED] Sorry, I see that you are working with Matlab since March 2011. If I have misunderstood the question, please explain again, what you expect set(handles.xy, 'string', 'bla') to do and what handles.xy is.

Image Analyst
Image Analyst on 10 Dec 2012
Where in the OpenFcn is handles.xy empty? For it to be empty that means it must exist (though I know that sounds contradictory). For example do you think that you assigned a control on the gui in GUIDE with the "tag" property called "xy"? And then when you set a breakpoint and examine what fields are attached to handles, you see xy, but is says it's empty? But like Jan says, if you try to call that set(handles.xy...) it will immediately throw and exception if there is no control with tag "xy" and thus you will not even be able to inspect it and see that it's empty. It's not empty - is just plain doesn't exist. So which is it? 1) you have a control with tag xy, or 2) no control with tag xy but you just mistakenly thought that you could attach a new xy field with handles with the set function instead of directly assigning it? What is xy anyway? Is that supposed to be a static text or edit box? Or is it just some variable you wanted to attach to handles to make it global? One final thought. If you did want to attach xy to handles and did it in the outputFcn, then you could use set() because it should exist in the OpenFcn function. The OutputFcn function gets run on startup (and shutdown) before the OpenFcn function gets run. Did you do any assignments in the OutputFcn function (this is doubtful since beginners rarely do this or even know why or when to make modifications to the OutputFcn function).

Shamir Alavi
Shamir Alavi on 11 Dec 2012
Edited: Shamir Alavi on 11 Dec 2012
Hi Felix,
I am new to GUIDE and I came across a similar problem a few days ago. However, I think I have solved mine. Here's the context (it might be helpful for a clear understanding) before I post my solution:
I have several check boxes in my GUI. I also have a function (say 'Mouse') that does stuff when I press a mouse button. My target was to clear the check boxes every time I click on the mouse. At first, I wrote this line inside 'OpeningFcn' and 'Mouse':
And this happened while executing the code >> "Attempt to reference field of non-structure array." (and other related errors)
While debugging, I found out that the handles for 'checkbox1' was getting overwritten by the handles of 'OutputFcn'
1. In the 'OpeningFcn', I copied 'handles' to a new variable 'handles1':
handles1 = handles;
2. Set 'handles1' as a 'global' variable inside the 'Mouse' function.
3. Changed the input argument 'handles' to 'handles1' for checkbox1 (for all the checkboxes actually). So it looks like this:
function checkbox1_Callback(hObject, eventdata, handles1)
bla bla
if get(handles1.checkbox1,'Value')
bla bla bla
Now it works fine. Hope this helps.
Shamir Alavi
Shamir Alavi on 11 Dec 2012
Yes, I just found out that one of the functions is ruining the master handles structure.
Thanks for pointing it out.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by