假设你有两个GUI叫一个叫gui_1,另一个叫gui_2,在gui_2中调用gui_1的参数或者结果。
首先在gui_1中进行参数输入操作:
(1)返回值只有一个的情况
在gui_1中的:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function gui_1_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = 数据或者变量名;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
在gui_1中的
function varargout = gui_1_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
其实程序本来的.m文件就是这样的,不用更改,如果数据或者变量是在同一个GUI中别的控件或执行中输入的,将数据或者变量名定义为全局变量就可,即:
global 数据或者变量名;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function gui_1_OpeningFcn(hObject, eventdata, handles, varargin)
global 数据或者变量名;
handles.output = 数据或者变量名
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
(2)返回值有多个以上的情况
基本以上面相同,
function gui_1_OpeningFcn(hObject, eventdata, handles, varargin)
下面任意一个地方输入
handles.output = 数据1或者变量名1;
handles.secend_output = 数据2或者变量名2;
handles.thrid_output = 数据3或者变量名3;
...........................
同时在
在gui_1中OutputFcn输入以下语句
function varargout = gui_1_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
varargout{2} = handles.secend_output;
varargout{3} = handles.thrid_output;
........................
依次类推,在同一个gui中的参数传递采用上述的设置为全局变量调用即可。
在gui_2中的buttonCallback下输入
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function pushbutton1_Callback(hObject, eventdata, handles)
a=gui_1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
则gui_1的结果就传递到a中。
同时对返回值为多参数的情况gui_2中则要输入语句改为
在gui_2中的buttonCallback下输入
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function pushbutton1_Callback(hObject, eventdata, handles)
[data1,data2,data3,....]=gui_1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
则gui_1的结果就传递到[data1,data2,data3,....]=中,分别对这些返回值进行操作即可。
方法2:
使用setappdata和getappdata两个函数。
----------------------------
SETAPPDATA(H, NAME, VALUE) sets application-defined data for
the object with handle H. The application-defined data,
which is created if it does not already exist, is
assigned a NAME and a VALUE. VALUE may be anything.
See also getappdata, rmappdata, isappdata.
----------------------------
H设置为0,表示matlab的全局句柄,而不是某个窗口或者控件的句柄。
value为,你需要传递的数据变量,我们喜欢用结构变量。
name为当前figB的窗体标识,也就是窗体的tag。
例如在figB里面
spec_data.length = 20;
spec_data.weight = 200;
setappdata(0,'figB',spec_data); %保存
以上代码放在figB里面需要保存设置参数的callback里面
然后在figB的
output 那个callback
function varargout = figB_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
%**********************return global varible
%varargout{1} = handles.output;
uiwait(gcf);
h=getappdata(0,'figB');
varargout{1}=h;
通过output里面的缺省变量varargout输出来。
在figA里面 就那样用 ret=figB;