How to pass a variable from one GUI to other GUI
3 次查看(过去 30 天)
显示 更早的评论
mohanish
2018-10-4
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?
采纳的回答
Adam Danz
2018-10-4
Rather than reinventing the wheel, here's a nice explanation.
31 个评论
mohanish
2018-10-5
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
2018-10-5
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
2018-10-8
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
2018-10-9
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
2018-10-9
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
2018-10-9
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
2018-10-10
[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
2018-10-11
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
2018-10-15
编辑:mohanish
2018-10-15
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
2018-10-15
编辑:Adam Danz
2018-10-15
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
2018-10-15
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
2018-10-15
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
2018-10-15
编辑:Adam Danz
2018-10-15
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
2018-10-15
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
2018-10-15
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
2018-10-17
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
2018-10-17
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
2018-10-17
编辑:Adam Danz
2018-10-18
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
2018-10-18
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
2018-10-18
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
2018-10-18
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
2018-10-18
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!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Maintain or Transition figure-Based Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)