How to pass data from one GUI to another?

38 views (last 30 days)
Mathieu on 24 Jan 2011
Commented: Valarie on 28 Oct 2015
Hi again,
I have a question concerning how to pass data among GUI's. Here is the thing : I have two GUI's, a main one and a sub one. During the opening of my main GUI, I pass that code:
set (handles.input_lambda,'String','1057e-9')
handles.lambda = str2double(get(handles.input_lambda,'String'));
where the new variable handles.lambda is created. It is initialized to 1057e-9. Then later in the code, it can be modified in the function corresponding to the input_lambda Edit Text zone...
Then I want to get the value contained in handles.lambda in a sub GUI which is called by pushing a button in the main GUI. In the opening function of the sub GUI, I use that code:
mainGUIFigureHandle = etireur_compresseur_gui;
mainGUIdata = guidata(etireur_compresseur_gui);
handles.lambda = str2double(get(mainGUIdata.input_lambda,'String'));
set (handles.input_lambda_sub,'String',num2str(handles.lambda));
The problem here is in the first line where the main GUI (etireur_compresseur_GUI) is called again and therefore executes the piece of code placed in its Opening Function. So if I want to modify the value lambda in the main GUI, as soon as I call the sub GUI my new value is erased and replaced by 1057e-9.
Any ideas on how to solve this?

Answers (4)

Doug Hull
Doug Hull on 24 Jan 2011
This video shows how I pass data from one GUI to another.
The short answer is I use setappdata and gettappdata to store data in the GUIs. I also store the handle of the main GUI in the secondary GUI, so it is easier to find later.
Valarie on 28 Oct 2015
Great video! Thank you!!

Sign in to comment.

Paulo Silva
Paulo Silva on 24 Jan 2011
I use this easy way for data sharing between GUIs
%In the end of OpeningFcn of Main GUI
%When you want to edit shared data you must get the handle
%write a local variable called MyData to SharedData, any type of data
%get SharedData and save it to a local variable called SomeDataShared
Don't forget to clean up the data shared in the CloseReqFcn of you main GUI
rmappdata(HandleMainGUI,'MySharedData') %do rmappdata for all data shared
Remember that your GUIs might try to getappdata that doesn't exist, you should first test if it does exist
if (isappdata(0,'HandleMainGUI') & isappdata(HandleMainGUI,'MySharedData'))
%get, set or rm appdata
%do something else, maybe loading default values into those variables

Matt Fig
Matt Fig on 24 Jan 2011
I am not clear on your question, but let me try to understand. You say the problem arises when your main GUI is called again. Thus it sounds like you first have a main GUI, call the sub GUI, then open another main GUI. If this is true, then I think you could solve this by making sure that your sub GUI has a tag, then checking to see if it already has a value for handles.lambda. Assume the tag property of your sub GUI is set to 'SubGui', then instead of what you have in the openfcn of your main GUI, put something like this:
if isempty(findall(0,'tag','SubGui'))
set (handles.input_lambda,'String','1057e-9')
handles.lambda = str2double(get(handles.input_lambda,'String'));
That way the variable handles.lambda will only be initialized if there is no sub GUI already existing.
If I have misunderstood your problem, you may need to just check if the variable exists prior to initializing it with the EXIST function. Something like:
if ~exist('handles.lambda','var')
set (handles.input_lambda,'String','1057e-9')
handles.lambda = str2double(get(handles.input_lambda,'String'));

Paulo F.
Paulo F. on 6 Aug 2014
I use a simple but not very efficient way, just I store the data in a file and then load it from the other GUI.
for that you can use save('temp.mat') which stores all the variables you have so far, and then in the other, when you want to recover it load('temp.mat').


Find more on Migrate GUIDE Apps in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!