How do I pass variables from one GUI to another

Hey,
I'm still struggling passing variables from one GUI to another by using handles.
I have created a GUI called "main" including a push button "OK" and an edit text box "a". Using "OK" will open a secong GUI "main2" which is supposed to return a variable "e".
function OK_Callback(hObject, eventdata, handles)
uiwait(main2); %Open main2
e = handles.e;
set(handles.a,'string',e);
end
In "main2" I have a variable "e" being calculated in a function "calculate" which is being called after typing a number into the edit text box taged "d". By debugging I found out that "Calculate" returns the correct value. All cool so far.
function e = calculate(c,d)
e = c + d;
end
function d_Callback(hObject, eventdata, handles)
c = str2double(get(handles.c,'string'));
d = str2double(get(handles.d,'string'));
handles.e = e; % All cool until here!
set(handles.e,'string',e);
guidata(hObject,handles); % Update handles structure
uiresume; % back to main
end
What I'm struggling with is: How do I get "e" (or its handle) from "main2" into "main"? Also set(handles.e,'string',e) is not working.
Anybody knows what's going on here?
Thank heaps for any help!

1 comentario

Robert
Robert el 10 de Mayo de 2017
Can you put some pictures with this app, please? Thank you!

Iniciar sesión para comentar.

 Respuesta aceptada

Jonathan LeSage
Jonathan LeSage el 7 de Nov. de 2013
You can use the setappdata and getappdata functions to pass values between UIs that you've developed. Since you're passing values between UIs, you want to store the values in the root so all other UIs can access them.
For example, after computing "e" in your main2 UI, you can store the value to the root:
setappdata(0,'evalue',e);
By specifying a UI object handle of "0", we are telling MATLAB to store this value in the root directory. To access this value in your other UI, simple use the getappdata function:
e = getappdata(0,'evalue');
Hope this help to get you started!

9 comentarios

Gerrit
Gerrit el 8 de Nov. de 2013
It works! Thanks a lot, Jonathan! This seems so much easier than using handles. I'm wondering about advantages using handles, though?!
Gerrit
Gerrit el 8 de Nov. de 2013
The ideas of both, setappdata/getappdata and handles, seem quite similar - if not the same - to me as in storing the value in a directory or structure and get it back from there when you need it in a different GUI.
KABMNZ KABMNZ
KABMNZ KABMNZ el 17 de Jun. de 2017
thanks a lot
VYSHAKH V NAIR
VYSHAKH V NAIR el 30 de Sept. de 2017
thanku
Khine zar
Khine zar el 17 de Mzo. de 2018
thank you ! your answer is so helpful for me !
Could anyone tell me what are the possible values of the "UI object handle", please? and its respective description? I set it to "0" (as you said here) in my code and it didn't work. I don't know why. Thank you so much
Wubie Engdew
Wubie Engdew el 10 de Ag. de 2019
Editada: Wubie Engdew el 10 de Ag. de 2019
write this code in UI one
setappdata(0,'name_of_variable_in_UI_One',e);
and this code in another UI
getappdata(0,'write this code in another UI ',e);
Jingyang Xie
Jingyang Xie el 28 de Feb. de 2021
Thank you Jonathan!
genius!!!!!!!!!!!

Iniciar sesión para comentar.

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by