How to pass data from one GUI to another?
8 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(4 个)
Doug Hull
2011-1-24
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
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...
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
0 个评论
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
0 个评论
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!
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!