Reputation: 565
I have two GUIs in MATLAB. I stored some values in GUI1 in the handles structure, so that when it displays in the Command Window, it looks like this:
GUI1: [1x1 Figure]
pushbutton2: [1x1 UIControl]
text2: [1x1 UIControl]
edit1: [1x1 UIControl]
output: [1x1 Figure]
val1: 0
I want to use val1
to set a value, counter
, in GUI2. I don't have any command to initialize counter
in GUI2. How do I access the handles of GUI1 in GUI2?
I tried to use the command guidata(findobj('Tag', 'GUI1')) to get those handles, but it shows me that it's empty.
I tried doing the following:
In GUI1, under OpeningFcn:
handles.val1 = 0;
guidata(hObject, handles);
setappdata(handles.GUI1,'val1', handles.val1)
And in GUI2, in a pushbutton function:
counter = getappdata(handles.GUI1,'val1')
But that doesn't seem to work either! It gives me an error saying, "Reference to non-existent field 'GUI1'."
I have the handle visibility on for GUI1, and a tag set to "GUI1". Why am I still having this issue?
Upvotes: 1
Views: 676
Reputation: 1894
You should set the GUI's Tag before finding object, i.e GUI1's Tag = GUI1
. Then you can try to find all children of root object:
gui1_H = get(0,'Children', 'Tag', 'GUI1');
or just use findobj
:
gui1_H = findobj('Type', 'figure', 'Tag', 'GUI1');
In some cases, the GUI's HandleVisibility
is set to off
, in this case, you can use findall
in your GUI2:
gui1_H = findall(0, 'Type', 'figure', 'Tag', 'GUI1');
And get the handle struct:
data = guidata(gui1_H);
disp(data.val);
Note that you are currently opening 2 GUIs at the same time, so if you keep both GUIs' default Tag to be figure1
, then gui1_H
will not be counted as graphic object's handle, hence you also cannot get its guidata
.
Upvotes: 2