Attempt to reference field of non-structure array

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

 Respuesta aceptada

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 comentarios

Sajid Afaque
Sajid Afaque el 17 de Abr. de 2019
thanks walter, its working.
how to do the samething if we have more than two gui's,say we have 3 gui's .
@Sajid: Exactly the same way:
fig1 = gcf(); %first GUI's figure handle
fig2 = SecondGUI(); %returns second GUI's figure handle
fig3 = ThirdGUI(); %returns second GUI's figure handle
handles2 = guidata(fig2); %fetch second GUI's handles
handles2.fig1 = fig1; %record first GUI's figure handle
handles2.fig3 = fig3;
guidata(fig2, handles2);
handles3 = guidata(fig3); %fetch third GUI's handles
handles3.fig1 = fig1; %record first GUI's figure handle
handles3.fig2 = fig2;
guidata(fig3, handles3);
Sajid Afaque
Sajid Afaque el 17 de Abr. de 2019
yeah thats perfectly fine walter,
but it is opening both second and third gui together.but ineed to open up in a sequence.....that is first gui opens second gui and second gui opens third .but i need first gui data in the third gui.
help me out please.
%in first gui
fig1 = gcf(); %first GUI's figure handle
handles1 = guidata(fig1);
fig2 = SecondGUI(); %returns second GUI's figure handle
handles2 = guidata(fig2); %fetch second GUI's handles
handles1.fig1 = fig1;
handles1.fig2 = fig2;
handles1.fig3 = [];
guidata(fig1, handles1);
handles2.fig1 = fig1; %record first GUI's figure handle
handles2.fig2 = fig2;
handles2.fig3 = [];
guidata(fig2, handles2); %and update second GUI's handles
%in second gui
function some_callback(hObject, event, handles2)
%in second GUI, handles refers to handles for second GUI
%and we stored a copy of the first GUI's figure handle there
fig1 = handles2.fig1;
handles1 = guidata(fig1); %retrieve handles for first GUI
fig2 = handles1.fig2;
fig3 = ThirdGUI(); %returns third GUI's figure handle
handles3 = guidata(fig3);
handles1.fig3 = fig3; %notify fig1 where fig3 is
guidata(fig1, handles1);
handles2.fig3 = fig3; %notify fig2 where fig3 is
guidata(fig2, handles2);
handles3.fig1 = fig1; %notify fig3 where fig1 and fig2 are
handles3.fig2 = fig2;
handles3.fig3 = fig3;
guidata(fig3, handles3);
Sajid Afaque
Sajid Afaque el 17 de Abr. de 2019
i am getting the below error
>> Gui1
Undefined function or variable 'handles2'.
Error in Gui2>close_button_Callback (line 97)
fig1 = handles2.fig1;
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Gui2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Gui2('close_button_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Notice in my code I named the third parameter of the callback as handles2 . I suspect you did not do that.
function close_button_Callback(hObject, eventdata, handles)
%in second GUI, handles refers to handles for second GUI
%and we stored a copy of the first GUI's figure handle there
handles2 = handles;
fig1 = handles2.fig1;
handles1 = guidata(fig1); %retrieve handles for first GUI
fig2 = handles1.fig2;
fig3 = ThirdGUI(); %returns third GUI's figure handle
handles3 = guidata(fig3);
handles1.fig3 = fig3; %notify fig1 where fig3 is
guidata(fig1, handles1);
handles2.fig3 = fig3; %notify fig2 where fig3 is
guidata(fig2, handles2);
handles3.fig1 = fig1; %notify fig3 where fig1 and fig2 are
handles3.fig2 = fig2;
handles3.fig3 = fig3;
guidata(fig3, handles3);
end
Sajid Afaque
Sajid Afaque el 17 de Abr. de 2019
yeah walter , i did miss that.
you are genious ,thanks a lot
Sajid Afaque
Sajid Afaque el 25 de Abr. de 2019
Editada: Jan el 25 de Abr. de 2019
hi
its working , i am able to read data between gui's ,but the handles are only available for that function.
what should i do if i have to read data from gui 1 and based on that date display specific data in gui 3 as soon as gui 3 opens, that is in opening fcn of gui3
[MOVED from flags] its a different related question
@Sajid: Please use flags only to inform admins and editors about inappropriate contents like rudeness or spam. Thanks.
You find all you need in this thread already. To obtain data from the GUI1 use:
GUI1H = findobj(groot, 'flat', 'Tag', 'GUI1_Tag');
Gui1Handles = guidata(GUI1H);
...
Use the tag you gave your GUI1.
The mutual dependency of different GUIs is confusing and counter-intuitive. Prefer to use a multi-tabbed GUI or a larger interface, such that all values belonging together appear together.
Sajid Afaque
Sajid Afaque el 30 de Abr. de 2019
ok i did not knew about flag.
it won't be repeated
Belmadnai issam
Belmadnai issam el 21 de Mayo de 2019
Editada: Belmadnai issam el 21 de Mayo de 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
Walter Roberson el 21 de Mayo de 2019
Editada: Walter Roberson el 21 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 11 de En. de 2016

Editada:

el 21 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by