Attempt to reference field of non-structure array
4 views (last 30 days)
Show older comments
Astrid Valle
on 11 Jan 2016
Edited: Walter Roberson
on 21 May 2019
Hi, I'm trying to open a gui from other gui, by separate both works fine, but when I open the second guide and get some values that says 'Attempt to reference field of non structure array Error T1=get(handles.T,'String') ' Please help me My code
function T..Callback(hObject,evendata,handles)
....
T1=get(handles.T,'String')
0 Comments
Accepted Answer
Walter Roberson
on 12 Jan 2016
Each handles structure is associated with a particular figure. When you are using GUIDE, the handles structure that would be automatically referenced on your behalf would belong to the figure (GUI) that the graphics object is part of. That handles structure is not going to know anything about the handles structure of the other figure (GUI) unless you specifically passed in that information.
When the first GUI calls upon the second GUI, the figure handle of the second GUI will be returned to the caller in the first GUI. The first GUI can then use guidata() on the second figure's figure handle in order to get the second GUI's handles, and the first GUI can then write the first GUI's figure handle into the second GUI's handles. Then in the second GUI, when something is needed out of the first GUI, that recorded figure handle can be used to retrieve the first GUI's handles. For example,
%in first gui
fig1 = gcf(); %first GUI's figure handle
fig2 = SecondGUI(); %returns second GUI's figure handle
handles2 = guidata(fig2); %fetch second GUI's handles
handles2.fig1 = fig1; %record first GUI's figure handle
guidata(fig2, handles2); %and update second GUI's handles
and in second GUI
function some_callback(hObject, event, handles)
%in second GUI, handles refers to handles for second GUI
%and we stored a copy of the first GUI's figure handle there
fig1 = handles.fig1;
handles1 = guidata(fig1); %retrieve handles for first GUI
get(handles1.T, 'String') %reference to something in first GUI
Alternately, in the case where there is only one object with the tag T, you can skip all of this and instead use
function some_callback(hObject, event, handles)
T = findall(0, 'Tag', 'T'); %come out come out where-ever you are!
get(T, 'String')
findall can search all objects in all figures. It is not as efficient as the code from above, and you have complications if there are multiple objects with the same Tag.
13 Comments
Belmadnai issam
on 21 May 2019
Edited: Belmadnai issam
on 21 May 2019
@Sajid: Please can you show me your example because it's not working i my GUIs
I have 4 GUIs it's and i want to pass data between it , it's complicated little bit, so please help me and thank you in advance
Walter Roberson
on 21 May 2019
Edited: Walter Roberson
on 21 May 2019
The easiest way is to give each of the figure() a unique tag, and then when you need to refer to items in another gui, use findobj like Jan showed https://www.mathworks.com/matlabcentral/answers/263419-attempt-to-reference-field-of-non-structure-array#comment_698061 to locate the handle of the other gui. Then you can guidata() that handle to get to its handle list.
More Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks 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!