現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How to pass a variable from one GUI to other GUI
1 回表示 (過去 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!
その他の回答 (1 件)
参考
カテゴリ
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)