Passing data between diferent GUIs
2 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Julien
2012-10-5
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)
更多回答(1 个)
Julien
2012-10-6
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!