Pass Data between Gui`s
24 次查看(过去 30 天)
显示 更早的评论
Hey Guys, I have created two GUI`s but i dont know how to pass Data between them. Both GUI`s are several m-files.
pls help me ....
0 个评论
采纳的回答
Geoff Hayes
2014-8-4
Max - there are probably several different ways to do this, so here is one. Let's assume that you have two GUIs named Gui1 and Gui2 and that they are distinct GUIs (i.e. you are not running two instances of the same one). In the Property Inspector for Gui1, set the HandleVisibility property to on, and the Tag property to Gui1. This will allow us to search for the GUI given its (unique) tag. Do the same for Gui2 (except name the tag Gui2). Save both.
Now suppose that in Gui2 you want access to some of the data in Gui1. What you require, is the handle to the latter. In a (say) push button callback of Gui2, you can add the following code
function pushbutton1_Callback(hObject, eventdata, handles)
% 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
g1data = guidata(h);
% maybe you want to set the text in Gui2 with that from Gui1
set(handles.text1,'String',get(g1data.edit1,'String'));
% maybe you want to get some data that was saved to the Gui1 app
x = getappdata(h,'x');
end
The above example tests out two ways in which we can get data from the other GUI - use either guidata which gives us the handles to all widgets of Gui1 AND any user-defined data that had been saved in Gui1 to the handles object as
function pushbutton3_Callback(hObject, eventdata, handles)
% in some Gui1 callback, we create the following xData field in the handles
% structure
handles.xData = -2*pi:0.0001:2*pi;
% now we save the data
guidata(hObject,handles);
We then access this data as shown previously.
The other way is to use the setappdata and getappdata pairings. Using the previous callback for Gui1, we can save the app data as
function pushbutton3_Callback(hObject, eventdata, handles)
% in some Gui1 callback, we create the following xData field in the handles
% structure
handles.xData = -2*pi:0.0001:2*pi;
% now we save the data
guidata(hObject,handles);
setappdata(handles.Gui1,'x',[1:53]);
and then retrieve it with getappdata.
NOTE how we use must use the handles.Gui1 to save/set the data. And that is it. Try the above and see what happens!
17 个评论
Ahmed Elsherif
2018-12-11
Hi, Geoff and thanks for your thoughts. I have made my code depeding on your explaination and I have a problem. What I want is to run the 1st gui then save its output with is a matrix with 1369 rows and different columns. The saving of the output in the first gui takes place with the following pushbutton:
fid = fopen([pathname,filename,'_result.dat'],'w');
fprintf(fid,[intinfotext,tauinfotext]);
fprintf(fid,frmtext,datatosave');
fclose(fid);
Afterwards, I need to extract the saved data into another the second gui in a list box, then to run the second gui. But by runing my code with your idea of
g1data = guidata(h)
set(handles.edit_tauexp,'string',x(:,1));
set(handles.edit_rexp,'string',x(:,2));
g1data shows me too many things and an error message:
Index exceeds matrix dimensions.
Error in gui3>pushbutton8_Callback (line 362)
set(handles.edit_tauexp,'string',x(:,1));
Would you please help me for that?
Geoff Hayes
2018-12-12
Ahmed - what can you tell us about x? What are the dimensions? Put a breakpoint at this line of code and then use the debugger to see what is going wrong.
更多回答(3 个)
Sara
2014-8-4
In GUI_1, store everything you need to share in handles, and call GUI_2 as:
GUI_2(handles)
This may go into a pushbutton callback, i.e. where you want to have GUI_2 appear. In the opening function of GUI_2, you variables will be in varargin{1}.you_var_name. You can now do (in GUI_2 opening function):
handles.myvar1 = varargin{1}.myvar1
to carry around variables from GUI_1 in GUI_2. You could also
The same works from GUI_2 to GUI_1.
4 个评论
Image Analyst
2019-3-9
Sharing between multiple GUIs. If the "main" GUI calls other GUIs, then the best way to do it is by passing variables in via the input argument list, and accepting output variables via the output argument list. The output argument of GUI2 can then be sent into GUI3. So someplace in GUI1 (like the callback function of the "Go!" button of GUI1), you'd have this
[out2a out2b out2c] = gui2(in2a, in2b, in2c, in2d);
[out3a out3b] = gui3(out2a, out2b);
or something along those lines. The arguments can be extracted out of the varargin cell array of your opening code for the GUI, for example in the GUI's "OpeningFcn" function if you used GUIDE. Once they are in your opening function, then they can be shared amongst the other functions in the GUI with the methods mentioned earlier in this section. This method will not let GUI1 control GUI2 and GUI3's parameters "live" - they can be changed only when calling the GUIs. To have GUI1 control GUI2 when GUI2 is already running, you can use the assignin() function.
This answer from Geoff Hayes in the Answers forum may also help you in the multiple GUI situation: [3]
Some advice regarding Sara's answer is to use another name for the handle in the second GUI. Call it handles2 or something, not handles
handles2 = varargin{1};
or you may not be able to control the controls in the second GUI because the handles in the first/calling GUI will have been overwritten by this new/incoming handles. For example, if you don't, handles.button1 may refer to the other/called gui's button1, not the gui in this gui's, the calling gui's, button1.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!