MATLAB: Attempt to reference field of non-structure array

handlesmatlab gui

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

Best Answer

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.