MATLAB Answers

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

22 views (last 30 days)
Felix
Felix on 10 Dec 2012
(I found almost the same problem in this question: http://www.mathworks.com/matlabcentral/answers/51874-how-do-i-create-handle-variables-when-initializing-a-gui, 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
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;
pause(2);
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.

  0 Comments

Sign in to comment.


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).

  0 Comments

Sign in to comment.


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:
Context:
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':
set(handles.checkbox1,'Value',0);
And this happened while executing the code >> "Attempt to reference field of non-structure array." (and other related errors)
Debug:
While debugging, I found out that the handles for 'checkbox1' was getting overwritten by the handles of 'OutputFcn'
Solution
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
end
Now it works fine. Hope this helps.
Shamir

  2 Comments

Image Analyst
Image Analyst on 11 Dec 2012
None of that should be necessary. Just don't overwrite the original handles structure - simple as that. Actually inside any function handles is a local copy - not the master handles - because MATLAB is "pass by value." So you can do whatever you want to handles inside that function as long as you don't call guidata in the event that you've ruined handles. In other words, you can totally destroy and mangle handles but as long as you don't call guidata, this mangled version of handles will not be copied to the master version and this local mangled version will vanish once the program exits.
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!