Passing data between diferent GUIs

2 views (last 30 days)
Luis
Luis on 4 Oct 2012
Commented: Zine on 4 Apr 2014
Hi,
I have a mainGUI and a constructorGUI, the first one exists to set the parameters for the secnd one, wich means i have to pass those parameters from multiple callbacks (on mainGUI) to the constructorGUI when I press a button.
On mainGUI I choose a puzzle to be done, collect all the data from that puzzle and its pieces, then i have to pass that data (coordinates of the pieces, locations of the files of every pieces, characteristics of the pieces,...) to the constructorGUI.
I was thinking to use setappdata and getappdata, I read a lot of information on that (setappdata on the mainGUI and getappdata on the constructorGUI) but I find myself trying to use that and getting all kinds of errors.
Can anyone explain me how to use this on TWO different GUIs and write a code example or if theres a better way to pass the data tell me about.
Thank you all!

Accepted Answer

Julien
Julien on 5 Oct 2012
Hi,
you have several method to share datas everywhere inside Matlab. The method you will choose depends on what kind of gui you are programing.
For programmed GUIs (without Guide), I use structures and nested functions (don't be afraid, when you understand the process it's very simple).
Process: 1) Creates your gui inside a main function,which will be the parent function. D 2) Define two structures : one to store handles, an other to store datas 3) create all the function you need (gui creation, callbacks definitions, ans other personal functions) 4) you just have to select the appropriate handles inside the handle struct. to select which object you want to modify (you can also add other structures inside handles structure, for example to separate handles of each structure, ex: handles.gui1, handles.gui2
Study the following code, which creates 2 guis: you enter a value inside the first, and send it with a push button to the second.
function main() % ' Parent function '
close all
handles=struct; %'Structure which stores all object handles
datas=struct; %'Structure which stores your datas
gui1; % call the function to create gui1
gui2; % call the function to create gui2
disp(handles) % just to show you what handle struct contain
function gui1 % First nested function
handles.fig1=figure('Position',[82 363 560 420]);
handles.edit1=uicontrol('Pos',[30 50 50 20],'Style','edit');
handles.pushbutton1=uicontrol('Pos',[90 50 100 20],'String','Send to GUI2');
set(handles.pushbutton1,'Callback',@send_datas_clbk);
end
function gui2 % Second nested function
handles.fig2=figure('pos',[82+700 363 560 420]);
handles.static1=uicontrol('Style','text');
end
% Callbacks function, that is a nested function too
function send_datas_clbk(hObject,Event)
datas.value1=get(handles.edit1,'String');
set(handles.static1,'String',datas.value1);
end
end
% End of file
If you use GUIDE, the method I will combines structures, setappdata and getappdata.
Like the first method, I create two structures that contains the handles and datas.
The idea is, whereas storing the structures inside the figures objects, store it inside the root !
setappdata(0,'MyStruct',Struct)
getappdata(0,'MyStruct')
If all your variables are stored inside datas struct, then you can acces to all datas anywhere in Matlab by just getting the data structure store in the root. (even accessible from base workspace)
  2 Comments
Luis
Luis on 6 Oct 2012
Hi Julien,
First of all Thank You, it worked just the way I wanted. I'm using Guide to do my GUIs. My mistake here was about where to store it, I saw somewhere written that 0 was the root and I tried to apply it before but somehow it didn't work, maybe because of a misspelled 'Mystruct' or Struct.
I have another question. What if I didn't want to store it in the root what else could I write instead of 0? I understood the 0 is the root but if I want something else on that field what could I use there, what is the meaning of that field? (the root works great for me this is just a curiosity so I can understand better this idea of set and getappdata)
Thank You again!
Zine
Zine on 4 Apr 2014
Thanks very much it is very perfect

Sign in to comment.

More Answers (1)

Julien
Julien on 6 Oct 2012
In fact, the first input for getappdata,setappdata, (and also set and get ) functions is a handle. It's a kind of pointer that contains the address (the ID) of a Matlab object.
The handle 0 correspond to the handle of the root.
If you define
fig=figure();
then, the variable fig will store the handle of the figure you have defined.
It's the same for each Matlab Object you create (figure,axes, uicontrols ...)
So, defining a structure that stores handles is a good way to share handles of all components you define.
In fact, GUIDE define by default a structure called handles which containes all handles of objects you have placed on the layout.
To acces a specific handle, type : handles.tag, which tag is the tag of the object you want to get handles.
insert somewhere in your GUI disp(handles) to see what the handles structure contains.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!