Hot to set fields from one GUI to another

Dear all,
I've been able to transfer variables from one gui to another, there's plenty of stuff around on that.
What I haven't been able to do is to set a field of a GUI from another GUI.
Here's an example: I have two GUIs opened: GUI1 and GUI2. GUI1 has a text box, GUI2 has a push button. Now, when I push the button on GUI2 I would like so set the string in the text box of GUI1 to something.
How can I achieve this?
Thanks!
Lorenzo

 Réponse acceptée

Mischa Kim
Mischa Kim le 21 Jan 2014

1 vote

Hello Lorenzo, check out this answer.

1 commentaire

Lorenzo
Lorenzo le 21 Jan 2014
Thanks a lot Mischa. I'm not sure the answer you posted is for the same issue I am having though (but I might be wrong...)
What I need is to have something like this in my sub_GUI: set(mainGui.edit,'String','My string')
Thanks again!
Lorenzo

Connectez-vous pour commenter.

Plus de réponses (3)

Jan
Jan le 21 Jan 2014
Modifié(e) : Jan le 21 Jan 2014
If you can provide variables from one GUI to the other, you can provide the handles struct also. And there you find the handles of the GUI objects. If your GUIs have the tags "GUI1" and "GUI2" (a bad choice, by the way), you can set the text of an object in GUI1 from a callback of GUI2:
function ButtonCallback(hObject, EventData, handles)
% Better do this once only!
gui1H = findobj(allchild(0), 'flat', 'tag', 'GUI1'); % [EDITED]
gui1Handles = guidata(gui1H);
set(gui1Handles.Button1, 'String', 'hello')
Add a TRY/CATCH for a meaningful error message, if the 2nd GUI has been closed before.

2 commentaires

Lorenzo
Lorenzo le 21 Jan 2014
Thanks Jan.
Quick question: as you said, this way I can set a textBox in GUI1 using a button in GUI2. To this extent, why is your findobj looking for GUI2 instead of GUI1 if the handle I need to set is GUI1?
Thanks again!
Jan
Jan le 21 Jan 2014
@Lorenzo: This was a typo. Fixed now.

Connectez-vous pour commenter.

Lorenzo
Lorenzo le 21 Jan 2014
Modifié(e) : Lorenzo le 21 Jan 2014
Mischa, Jan,
I found my answer in Mischa's link: in my GUI2 I now have:
function pushbutton1_Callback(hObject, eventdata, handles)
mainGUIdata = guidata(GUI1);
set(mainGUIdata.edit1, 'String', 'Hello')
and this is working.
Now, what is the purpose of
gui2H = findobj(allchild(0), 'flat', 'tag', 'GUI2');
in Jan's reply?
Thanks again for your time.

4 commentaires

Jan
Jan le 21 Jan 2014
Modifié(e) : Jan le 21 Jan 2014
In the code you have posted, the variable GUI1 seems to be undefined. Beside the typo, that in my code GUI2 and GUI1 have been confused, the FINDOBJ is used to find the handle of the other GUI. It would be better to obtain this handle e.g. when the other figure is created.
Lorenzo
Lorenzo le 21 Jan 2014
GUI1 is the name of my first GUI (ie I call it with GUI1.m). It works apparently... This is not a good way to achieve that?
The fact is no matter what I keep getting:
gui1H =
Empty matrix: 0-by-1
Lorenzo
Lorenzo le 22 Jan 2014
Sorry to keep adding stuff... it appears to be working now, I had to use My GUI's tag in the place of 'GUI1', still, I would be interested if there was a more direct way to do this when my gui is started

Connectez-vous pour commenter.

Lorenzo
Lorenzo le 22 Jan 2014

0 votes

I guess it doesn't work eventually... when I do guidata(GUI1) it appears to be working but indedd it runs GUI1 from start back again.... Any advise?
Thanks!

1 commentaire

Lorenzo
Lorenzo le 22 Jan 2014
Meaning: as Jan suggested, how can I get the handle of a gui once this is launched? SOmething like:
myGuiHandle=GUI1

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by