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

Adam
Adam 2018년 2월 8일
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
Adam 2018년 2월 9일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2018년 2월 8일
편집: Jan 2018년 2월 8일

0 개 추천

  1. Do not use "ans" as a variable. It is overwritten automatically by any uncaught output of a Matlab function.
  2. 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)
  3. 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

Joe Sitinjak
Joe Sitinjak 2018년 2월 9일
okay brother, thanks for the advice and the answer. let me try that..
Le Dung
Le Dung 2018년 12월 26일
I use your code, matlab return:
H must be the handle to a figure or figure descendent.
Jan
Jan 2018년 12월 28일
@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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

질문:

2018년 2월 8일

댓글:

Jan
2018년 12월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by