How to pass data from one GUI to another?

조회 수: 16 (최근 30일)
Mathieu
Mathieu 2011년 1월 24일
댓글: Valarie 2015년 10월 28일
Hi again,
I have a question concerning how to pass data among GUI's. Here is the thing : I have two GUI's, a main one and a sub one. During the opening of my main GUI, I pass that code:
set (handles.input_lambda,'String','1057e-9')
handles.lambda = str2double(get(handles.input_lambda,'String'));
where the new variable handles.lambda is created. It is initialized to 1057e-9. Then later in the code, it can be modified in the function corresponding to the input_lambda Edit Text zone...
Then I want to get the value contained in handles.lambda in a sub GUI which is called by pushing a button in the main GUI. In the opening function of the sub GUI, I use that code:
mainGUIFigureHandle = etireur_compresseur_gui;
mainGUIdata = guidata(etireur_compresseur_gui);
handles.lambda = str2double(get(mainGUIdata.input_lambda,'String'));
set (handles.input_lambda_sub,'String',num2str(handles.lambda));
The problem here is in the first line where the main GUI (etireur_compresseur_GUI) is called again and therefore executes the piece of code placed in its Opening Function. So if I want to modify the value lambda in the main GUI, as soon as I call the sub GUI my new value is erased and replaced by 1057e-9.
Any ideas on how to solve this?
Thanks

답변 (4개)

Doug Hull
Doug Hull 2011년 1월 24일
This video shows how I pass data from one GUI to another.
The short answer is I use setappdata and gettappdata to store data in the GUIs. I also store the handle of the main GUI in the secondary GUI, so it is easier to find later.
  댓글 수: 6
Max Müller
Max Müller 2014년 8월 6일
can u pls tell my why i have to download this video. I am just a student at the IPP ....so i have no access to download things...
Valarie
Valarie 2015년 10월 28일
Great video! Thank you!!

댓글을 달려면 로그인하십시오.


Paulo Silva
Paulo Silva 2011년 1월 24일
I use this easy way for data sharing between GUIs
%In the end of OpeningFcn of Main GUI
setappdata(0,'HandleMainGUI',hObject);
%When you want to edit shared data you must get the handle
HandleMainGUI=getappdata(0,'HandleMainGUI');
%write a local variable called MyData to SharedData, any type of data
setappdata(HandleMainGUI,'SharedData',MyData);
%get SharedData and save it to a local variable called SomeDataShared
SomeDataShared=getappdata(HandleMainGUI,'SharedData');
Don't forget to clean up the data shared in the CloseReqFcn of you main GUI
HandleMainGUI=getappdata(0,'HandleMainGUI');
rmappdata(HandleMainGUI,'MySharedData') %do rmappdata for all data shared
Remember that your GUIs might try to getappdata that doesn't exist, you should first test if it does exist
if (isappdata(0,'HandleMainGUI') & isappdata(HandleMainGUI,'MySharedData'))
%get, set or rm appdata
else
%do something else, maybe loading default values into those variables
end

Matt Fig
Matt Fig 2011년 1월 24일
I am not clear on your question, but let me try to understand. You say the problem arises when your main GUI is called again. Thus it sounds like you first have a main GUI, call the sub GUI, then open another main GUI. If this is true, then I think you could solve this by making sure that your sub GUI has a tag, then checking to see if it already has a value for handles.lambda. Assume the tag property of your sub GUI is set to 'SubGui', then instead of what you have in the openfcn of your main GUI, put something like this:
if isempty(findall(0,'tag','SubGui'))
set (handles.input_lambda,'String','1057e-9')
handles.lambda = str2double(get(handles.input_lambda,'String'));
end
That way the variable handles.lambda will only be initialized if there is no sub GUI already existing.
If I have misunderstood your problem, you may need to just check if the variable exists prior to initializing it with the EXIST function. Something like:
if ~exist('handles.lambda','var')
set (handles.input_lambda,'String','1057e-9')
handles.lambda = str2double(get(handles.input_lambda,'String'));
end

Paulo F.
Paulo F. 2014년 8월 6일
I use a simple but not very efficient way, just I store the data in a file and then load it from the other GUI.
for that you can use save('temp.mat') which stores all the variables you have so far, and then in the other, when you want to recover it load('temp.mat').
Cheers!

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by