You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to pass a variable from one GUI to other GUI
3 views (last 30 days)
Show older comments
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
Adam Danz
on 4 Oct 2018
Rather than reinventing the wheel, here's a nice explanation.
31 Comments
mohanish
on 5 Oct 2018
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.
Adam Danz
on 5 Oct 2018
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.
mohanish
on 8 Oct 2018
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
Adam Danz
on 9 Oct 2018
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.
mohanish
on 9 Oct 2018
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'
Adam Danz
on 9 Oct 2018
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.
mohanish
on 10 Oct 2018
[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);
Adam Danz
on 11 Oct 2018
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.
mohanish
on 15 Oct 2018
Edited: mohanish
on 15 Oct 2018
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
Adam Danz
on 15 Oct 2018
Edited: Adam Danz
on 15 Oct 2018
Ok, now I can see the source of the error.
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');
Adam Danz
on 15 Oct 2018
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.
mohanish
on 15 Oct 2018
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
Adam Danz
on 15 Oct 2018
Edited: Adam Danz
on 15 Oct 2018
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.
Adam Danz
on 15 Oct 2018
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().
Adam Danz
on 15 Oct 2018
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".
mohanish
on 17 Oct 2018
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?
Adam Danz
on 17 Oct 2018
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')
Adam Danz
on 17 Oct 2018
Edited: Adam Danz
on 18 Oct 2018
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, ......................)
Adam Danz
on 18 Oct 2018
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.
mohanish
on 18 Oct 2018
No, I mean should I write this code..
guiHandle = findobj('Name', 'multiply1', 'tag', 'Gui1');
in command window or in my actual(multiply1) code?
Adam Danz
on 18 Oct 2018
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.
mohanish
on 18 Oct 2018
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!
More Answers (1)
See Also
Categories
Find more on Maintain or Transition figure-Based 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)