passing data between gui
Afficher commentaires plus anciens
hello, i need your help please,, i just tried on making 2 gui, i have a problem from main gui (the calculate) to sub gui (the result)
Gui_1 (main gui)
a=str2num(get(handles.edit1,'string'));
b=str2num(get(handles.edit2,'string'));
ans=a+b;
setappdata(0,'test_1',ans);
test_3 %calling the subgui (gui2)
Gui_2 (subgui)
b=getappdata(0,'test_3');
set(handles.edit1,'string',b);
string: 4
test_1: 1
test_3: 2
but the result didn't show in the panel edit1.. which part that i did wrong? thank u
2 commentaires
Adam
le 8 Fév 2018
It's not obvious from your formatting where
test_3
fits in, but you seem to be setting 'test_1' in appdata and then retrieving 'test_3' from appdata which you don't appear to have set anywhere.
Adam
le 9 Fév 2018
If you want a more complicated method to do this, but one which keeps each GUI's data properly encapsulated within the GUI and has each GUI respond to events in an underlying object rather then the method I use is mostly described in
There are always improvements that can be made, but the idea is that GUIs should not be talking directly to each other or fishing out each other's data, they should each simply be responding to and updating some other object which encapsulates the relevant data.
A GUI should simply be a window onto a program from which you trigger events or functions and update underlying parameters. It should do and store as little as possible itself, simply pass a parameter down to another object and, where required, listen for events from that object to update itself accordingly.
Réponses (1)
- Do not use "ans" as a variable. It is overwritten automatically by any uncaught output of a Matlab function.
- Global variables are a bad programming pattern. The ApplicationData of the Root object are global data. (search for "global variable" to find thousands of corresponding discussions in the net)
- Solution:
Use the handles of the other GUI to share data. E.g.:
function CreateGUI1
handles.FigH = figure('Tag', 'GUI1');
handles.Data = 19;
guidata(handles.FigH, handles);
end
function CreateGUI2
handles.FigH = figure('Tag', 'GUI2');
handles.ButtonH = uicontrol('Style', 'PushButton', 'Callback', @ButtonCB);
guidata(handles.FigH, handles);
end
function ButtonCB(ButtonH, EventData)
GUI1H = findobj(allchild(groot), 'flat', 'Tag', 'GUI1');
handlesGUI1 = guidata(GUI1H);
data = handelsGUI.Data;
set(ButtonH, 'String', sprintf('%g', data));
end
This shows how to use the tag of a GUI to find its handle. Then the contents of the handles struct of the other figure can be used to access its data.
Search in the forum for "share data between callbacks", whereby the callback can even belong to different figures.
3 commentaires
Joe Sitinjak
le 9 Fév 2018
Le Dung
le 26 Déc 2018
I use your code, matlab return:
H must be the handle to a figure or figure descendent.
Jan
le 28 Déc 2018
@Le Duong: Please post your code and the complete error message. The error message is clear: You provide a variable "H", which is not a graphics handle. But without seeing the code, I cannot guess, what it is instead.
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!