How to pass a variable from one GUI to other GUI

I am writing code in which I have multiple GUI interlinked with each other. I want a variable data from GUI1 to be used in GUI2. It is basically GMM data from GUI1 that i need to transfer to GUI2. In GUI1 : I have this data.. [d]=gmm_estimate(v (:,no_coeff)',No_of_Gaussians, No_of_iterations);
this generates matrix of coefficients I want this data to used in GUI2.. I want these coefficients to compare with the coefficients of GUI2 How do I call or transfer this data to GUI2?

 Accepted Answer

31 Comments

it is not working. i am saving the data from gui1 in a variable called (n). and i am calling the gui1 data as mentioned in the above link. it is showing me an error as the variable n is undefined.
You'll need to be more specific for me to help you. How are you saving the data from GUI1 and how are you grabbing the data from GUI1? Sharing the relevant code and the full error message is usually really helpful.
here are the two .m files for two Gui's. I want to use the data of multiply1 in Record.m file. I want to use the data in line 287 of multiply1 file in record.m file
I cannot run the GUI without the .fig file. But that's not what I was looking for anyway. Digging through the whole GUI to reverse engineer what's going on is a lot of work. Rather than that, please just cut and paste the code that saves the data in GUI1 and collects it in GUI2. Also please share the full error message.
Gui1: v=melcepst(data); ax2 = subplot('position', [0.07, 0.15, 0.4, 0.3]); plot(v); title ('Mel-Cepstra')
%%%%%%%%% Frequency Domain
ax2 = subplot('position', [0.56, 0.15, 0.4, 0.3]); plot(data); title ('Frequency Domain') xlabel('f(Hz)') ylabel('P1(f)')
No_of_Gaussians = 32;
No_of_iterations = 20;
no_coeff=[1:12];
_[mu_user1,sigma_user1,c_user1]=gmm_estimate(v (:,no_coeff)',No_of_Gaussians, No_of_iterations); I want save this variable [mu_user1,sigma_user1,c_user1]
Gui2: This is how i am calling that variable in Gui2 %%%%%% Multigauss
% get the handle of Gui1 h = findobj('Tag','Gui1'); % if exists (not empty) if ~isempty(h) % get handles and other user-defined data associated to Gui1 Y = guidata(h);
% maybe you want to get some data that was saved to the Gui1 app
getappdata(h,'mu_user1,sigma_user1,c_user1');
end
[lYM,lY]=lmultigauss(d(:,no_coeff)',mu_user1,sigma_user1,c_user1);
graph_gmm(d(:,no_coeff)',mu_user1,sigma_user1,c_user1);
Error: It is showing an error saying 'Undefined function mu_user1'
Please format the code using the { } button so it can be read properly.
I looked through your code briefly but not in detail since it's not formatted correctly. I see you are using getappdata() but I don't see where you are using setappdata(). Please have another look at the example in the link I provide and make sure you're using both of those functions properly.
[mu_user1,sigma_user1,c_user1]=gmm_estimate(v (:,no_coeff)',No_of_Gaussians, No_of_iterations);
% now we save the data
guidata(hObject,handles);
setappdata(handles.Gui1,'mu_user1,sigma_user1,c_user1',[1:53]);
This is my setappdata code. I want to save the [mu_user1,sigma_user1,c_user1] variable and use this data in Gui2.
Gui2................
h = findobj('Tag','Gui1'); % if exists (not empty) if ~isempty(h) % get handles and other user-defined data associated to Gui1 Y = guidata(h);
% maybe you want to get some data that was saved to the Gui1 app
%% Getappdata used here....
m =getappdata(handles,Gui1,'mu_user1,sigma_user1,c_user1');
I want this data from Gui1 and data calculated in Gui2 and plot both of them together.. here is the code I used to plot them together....
[lYM,lY]=lmultigauss(d(:,no_coeff)',m (1:3));
graph_gmm(d(:,no_coeff)',m);
I'm sorry, I can't help if the code isn't formatted. To format it, edit your comment and use the {} code button. I'd be happy to help but I can't easily read the unformatted code.
if true
No_of_Gaussians = 32;
No_of_iterations = 20;
no_coeff=[1:12];
[mu_user1,sigma_user1,c_user1]=gmm_estimate(v (:,no_coeff)',No_of_Gaussians, No_of_iterations);
setappdata(Gui1, 'mu_user1,sigma_user1,c_user1',mu_user1,sigma_user1,c_user1)
end
I want to use the data stored in the variable [mu_user1,sigma_user1,c_user1]
i am using the following code in Gui2 (record) to get that data..
if true
[d]=gmm_estimate(c (:,no_coeff)',No_of_Gaussians, No_of_iterations);
f= getappdata(Gui1,'[mu_user1,sigma_user1,c_user1]');
end
Ok, now I can see the source of the error.
Please read and understand how to use setappdata(). Documentation is here (<-link).
You can only assign one variable at a time using setappdata() and you're trying to assign all three at once. Furthermore, you're only using one long string as a variable name.
If you want to assign 3 variables, you can either assign them independently (method 1) or in a cell array (method 2).
Method 1
setappdata(Gui1, 'mu_user1',mu_user1);
setappdata(Gui1, 'sigma_user1',sigma_user1)
setappdata(Gui1, 'c_user1', c_user1)
Method 2
setappdata(Gui1, 'allVars', {mu_user1, sigma_user1, c_user1});
Then you can access those data using
getappdata(Gui1, 'mu_user1');
in setappdata code, Gui1 is the name of the tag right?
I got Gui1 from your code assuming it's the handle to your GUI or whatever object you're storing the data in.
Please read the documentation in the link I provided. It's short and you need to understand what these functions do and their inputs. The documentation even has an example that will be easy to follow.
I read the document. I edited my Gui tag name as 'Gui1', but when i am using the getappdata code it is showing me an error as:
if true
Undefined function or variable 'Gui1'.
Error in Record>record_Callback (line 123)
mu_user1= getappdata(Gui1, 'mu_user1');
end
That's because the first input to these function is a handle to a graphics object, not a tag. In your case, it will be the handle to your GUI. If your GUI is called 'myGUI', when you open it,
Gui1 = myGUI(); %open your gui
Then use Gui1 handle as the first input to the set.. and get... functions.
if true
% now we save the data
Gui1= multiply1();
setappdata(Gui1, 'mu_user1',mu_user1);
setappdata(Gui1, 'sigma_user1',sigma_user1)
setappdata(Gui1, 'c_user1', c_user1)
end
Using this, opens the same gui again.. I am confused..
The name of my first gui is 'multiply1' and the second is 'record'
The first input to setappdata() is the handle to your gui.
You can get the handle to your GUI several ways. One way is to save the handle when you open your GUI.
Another way is to use findobj() or findall().
mohanish
mohanish on 15 Oct 2018
Edited: mohanish on 15 Oct 2018
is the handle same as the name of the Gui? in my case multiply1?
what should i write in the setappdata code?
No, your GUI is an object and all objects have handles.
To learn how to get then handle to your GUI, I suggest you use google or this forum to search for "how to get handle of an running gui".
Hey Adam, I am not able to figure this out. I am stuck with this handle problem. Is there a way you can check my code and recommend what will be the handle that I need to use in getappdata code?
I can suggest a method but first I need some information.
Select the GUI that stores the information you need. Make sure that GUI is the active figure (by clicking on it). Then tell me what the output is for these two lines of code:
get(gcf, 'name')
get(gcf, 'tag')
mohanish
mohanish on 17 Oct 2018
Edited: mohanish on 17 Oct 2018
name- multiply1
tag- Gui1
Is there any special code to retrieve a data from 1 Gui to another?
Ok, Here's how you can get the handle to your gui.
guiHandle = findobj('Name', 'multiply1', 'tag', 'Gui1');
or just
guiHandle = findobj('Name', 'multiply1');
Now you can use
setappdata(guiHandle, .....................)
In the other GUI, you'll need to get the handle again and then use getappdata().
guiHandle = findobj('Name', 'multiply1', 'tag', 'Gui1');
getappdata(guiHandle, ......................)
should i write this code in the command window to get the handle?
The setappdata() code should be placed somewhere in your 'multiply1' GUI, wherever you need to store that data.
The getappdata() code should be placed somewhere in your 2nd GUI wherever you need to retrieve that data.
No, I mean should I write this code..
guiHandle = findobj('Name', 'multiply1', 'tag', 'Gui1');
in command window or in my actual(multiply1) code?
Mohanish, I think you're relying too much on me to tell you what to do rather than thinking through the problem, understanding the code, and finding a solution for yourself.
The function findobj() gives you the handle to your GUI. That handle needs to be passed into the setappdata() and getappdata() functions. So, wherever you're calling the setappdata() and getappdata() functions, you'll need to get the handle to your GUI.
Alright! I got it! I read some documents and figured it out. I am successful in passing the data to my other Gui. Thank you so much for your help Sir!

Sign in to comment.

More Answers (1)

In GUI1, where d is the data that you want to transfer:
setappdata(0,'mydata',d);
IN GUI2:
d = getappdata(0,'mydata');
See:
This might not work for MATLAB versions from R2014b.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 4 Oct 2018

Commented:

on 18 Oct 2018

Community Treasure Hunt

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

Start Hunting!