在Matlab中从不同的GUI调用GUI函数 [英] Calling GUI function from different GUI in Matlab
问题描述
我有两个GUI文件,如下所示:
I have two GUI files, which are as follows:
gui1.m
function varargout = gui1(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui1_OpeningFcn, ...
'gui_OutputFcn', @gui1_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function gui1_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = gui1_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function text_Callback(hObject, eventdata, handles)
function text_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function pushbutton1_Callback(hObject, eventdata, handles)
a = get(handles.text,'String');
disp(a);
此gui1.m
具有一个编辑框和一个按钮,当我单击pushbutton1
时,它将显示其内容.现在,我尝试使用以下代码对不同的GUI界面执行相同的操作.
This gui1.m
has a edit box and a button, when ever I click on the pushbutton1
it displays the content of it. Now I am trying to do the same with a different GUI interface with the following code.
gui2.m
function varargout = gui2(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui2_OpeningFcn, ...
'gui_OutputFcn', @gui2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function gui2_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = gui2_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
gui1('pushbutton1_Callback', hObject, eventdata, handles);
因此,当我单击gui2.m
的按钮pushbutton1
时,出现以下错误
so when I click on the button pushbutton1
of gui2.m
I get the following error
Reference to non-existent field 'text'.
Error in gui1>pushbutton1_Callback (line 44)
a = get(handles.text,'String');
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in gui1 (line 17)
gui_mainfcn(gui_State, varargin{:});
Error in gui2>pushbutton1_Callback (line 32)
gui1('pushbutton1_Callback', hObject, eventdata,
handles);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in gui2 (line 16)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)gui2('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
谁能告诉我问题出在哪里?因为当我删除set
并将其分配给gui1.m中的变量a
并按gui2.m中的按钮时,我能够显示a的值.
Could anyone tell me what the problem is? because when i remove the set
this and assign some number to variable a
in gui1.m and press the button in gui2.m I am able to display the value of a.
推荐答案
您需要在两个GUI之间共享数据:将要共享的数据(gui1
的本地数据)传递给共享变量(即gui1
的pushbutton1_Callback
函数中,只需调用gui2;
而不是gui1('pushbutton1_Callback', hObject, eventdata, handles);
.然后,在gui2
的pushbutton1_Callback
中,您可以检索共享数据MySharedData
并对数据进行所需的操作.
You need a shared data between two GUIs: pass the data (local data of gui1
) that you want to share to a shared variable (i.e MySharedData
) in gui1
's pushbutton1_Callback
function, and just call gui2;
instead of gui1('pushbutton1_Callback', hObject, eventdata, handles);
. Then in gui2
's pushbutton1_Callback
you can retrieve the shared data MySharedData
and do what you want with the data.
有一些说明:
http://www.mathworks.com/help/matlab/creating_guis/share-data-among-callbacks.html
http: //www.mathworks.com/matlabcentral/answers/338-how-to-pass-data-from-one-gui-to-other
更新:
更简单的方法,但我认为不太可靠:当调用gui1
的按钮时,将值分配给工作区的变量(assignin('base', 'varname', value)
),然后在gui2
的按钮CallbackFcn中可以获取变量与val = evalin('base', 'varname')
.
More easier way but in my opinion less reliable: when you call gui1
's pushbutton, assign the value to a workspace's variable (assignin('base', 'varname', value)
), then in gui2
's pushbutton CallbackFcn you can get the variable with val = evalin('base', 'varname')
.
这篇关于在Matlab中从不同的GUI调用GUI函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!