我有两个GUI文件,如下所示:
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界面。
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);
所以当我点击pushbutton1
的{{1}}按钮时,我收到以下错误
gui2.m
有人能告诉我这是什么问题吗?因为当我删除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
这个并在gui1.m中为变量set
分配一些数字并按下gui2.m中的按钮时,我能够显示a的值。
答案 0 :(得分:1)
您需要两个GUI之间的共享数据:将您要共享的数据(gui1
的本地数据)传递到MySharedData
中的共享变量(即gui1
)&# 39; s pushbutton1_Callback
功能,只需拨打gui2;
而不是gui1('pushbutton1_Callback', hObject, eventdata, handles);
。然后在gui2
pushbutton1_Callback
中,您可以检索共享数据MySharedData
,并根据数据执行您想要的操作。
有一些说明:
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-another
更新:
更简单的方法,但在我看来不太可靠:当您拨打gui1
的按钮时,将值分配给工作区的变量(assignin('base', 'varname', value)
),然后在{ {1}}按钮CallbackFcn您可以使用gui2
获取变量。