MATLAB: Passing data between diferent GUIs

data manipulationgetappdataguiguideMATLABpassing data between guissetappdata

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!

Best Answer

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)
Related Question