이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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!
참고 항목
카테고리
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 (한국어)