Borrar filtros
Borrar filtros

Pass Data between Gui`s

33 visualizaciones (últimos 30 días)
Max Müller
Max Müller el 4 de Ag. de 2014
Comentada: Image Analyst el 9 de Mzo. de 2019
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 ....

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 4 de Ag. de 2014
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
% maybe you want to get some data that was saved to the Gui1 app
x = getappdata(h,'x');
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
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
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 comentarios
Ahmed Elsherif
Ahmed Elsherif el 11 de Dic. de 2018
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');
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)
g1data shows me too many things and an error message:
Index exceeds matrix dimensions.
Error in gui3>pushbutton8_Callback (line 362)
Would you please help me for that?
Geoff Hayes
Geoff Hayes el 12 de Dic. de 2018
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.

Iniciar sesión para comentar.

Más respuestas (3)

Sara el 4 de Ag. de 2014
In GUI_1, store everything you need to share in handles, and call GUI_2 as:
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 comentarios
Xi Chen
Xi Chen el 9 de Mzo. de 2019
Thank you very much! I was looking for this answer for half a day.
Image Analyst
Image Analyst el 9 de Mzo. de 2019
This is also covered in the FAQ.
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.

Iniciar sesión para comentar.

Max Müller
Max Müller el 5 de Ag. de 2014

Image Analyst
Image Analyst el 6 de Ag. de 2014


Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.


Community Treasure Hunt

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

Start Hunting!

Translated by