Matlab figure传入数据到figure(一)
在利用Matlab编写GUI时,经常会碰到一个figure需要另一个figure中的计算数据的问题,这就涉及到不同figure之间的数据传递问题。除了通过global申明全局变量外,在此本文介绍利用handles来实现figure数据传入另一个figure。
1、figure的调用
调用一个figure即会打开并初始化一个figure。任何一个figure的在初始化之前都会执行如下代码(假设figure为Result):
function varargout = Result(varargin) % RESULT MATLAB code for Result.fig % RESULT, by itself, creates a new RESULT or raises the existing % singleton*. % % H = RESULT returns the handle to a new RESULT or the handle to % the existing singleton*. % % RESULT('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in RESULT.M with the given input arguments. % % RESULT('Property','Value',...) creates a new RESULT or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before Result_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to Result_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help Result % Last Modified by GUIDE v2.5 12-Jun-2017 15:43:14 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @Result_OpeningFcn, ... 'gui_OutputFcn', @Result_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 % End initialization code - DO NOT EDIT上面的代码给出了figure的几种打开(或调用)方式:
(1)Result,直接打开一个新的figure或者使得之前已经打开的figure窗口重新成为焦点窗口
(2)H = Result,直接打开一个新的figure或者使得之前已经打开的figure窗口重新成为焦点窗口并返回该figure的句柄
(3)Result('Result.m文件中某个函数的名字', hObject, eventdata, handles, ...),查找Result.m文件中是否有对应名称的函数,然后将该函数作为该figure的Callback函数并调用该函数,该方法不能打开figure或让已经打开的figure重新成为焦点窗口。(该方法可以在m文件外部调用m文件内部的函数)。另外这些参数会通过varargin变量传递到对应的OpeningFcn函数中。
(4)Result('Property1', 'val1', 'Property2', 'val2', ...)直接打开一个新的figure或者使得之前已经打开的figure窗口重新成为焦点窗口,在figure对应的OpeningFcn函数调用之前将这些“键值对”传递到GUI中。这些“键值对”参数修改对应的figure的界面设置情况(figure的Property Inspector中的对应的属性值)。另外这些“键值对”参数会通过varargin变量传递到对应的OpeningFcn函数中。
从上述的(3)和(4)可知,我们可以将参数经过varargin传入到figure中,并让该figure继续对传入的数据进行处理。那么varargin是什么呢?接着往下看。
2、varargin
在matalb中,varargin表示函数的变长度的参数列表,一般申明在函数参数的最后一个,用来收集函数的剩余的输入变量。
比如定义function func1(x, y, varargin),而采用func1(1, 2, 3, 4)调用该函数时varargin{1} = 3,varargin{2} = 4;当采用func1(1, 2, 3)调用该函数时varargin{1} = 3。
3、figure数据传入figure
经过上面的介绍(3)和(4)我们知道,在打开一个figure时,可以将数据传入到figure中。那到底怎么样传入的呢?
对应情况(3):
该情况下,我们被传入参数的figure只能调用对应的callback函数而不能打开该figure。另外需要注意的是:传入的第一个参数为某个函数的名称,第二个参数或者为空或者必须为一个对象句柄hObject或者handles,并且这些参数需要跟callback函数的参数列表对应。
为了说明使用方法,如上图所示,StrCat.fig将字符串1和字符串2进行组合,然后通过单击[组合]按钮将这两个字符串进行组合。在该[组合]按钮的callback函数下,向Result.fig传递一个组合后的字符串。为了能够向Result.fig传递参数,此时我们将组合后的字符串保存到StrCat.fig内的handles结构体中,然后向对应的函数传递该handles句柄。该[组合]按钮对应的callback函数如下:
% --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles)%StrCat.fig中的[组合]按钮的Callback函数 % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) str1 = get(handles.edit1,'string'); str2 = get(handles.edit2,'string'); resultStr = strcat('Welcome,', str1, ' ', str2); handles.resultStr = resultStr;%保存组合后的字符串到handles guidata(hObject,handles); Result('dispAAA',handles);%向Result.fig传递参数,其中dispAAA为Result.fig下的函数,handles为StrCat.fig的句柄
Result.m文件中的dispAAA()函数如下:
function dispAAA(handles)
disp('run function dispAAA()...');
disp('the reconstructed string is:');
disp(handles.resultStr);%handles为StrCat.fig的句柄,此时已经将StrCat.fig的信息传递过来了
由于该种情况下只能调用对应的callback函数而不能打开Result.fig,因此通过命令行输出其拼接后的字符串,具体效果如下:
其实上述介绍的并非真正意义上的figure之间的信息传入。因为Result.fig根本没有出现。可以对上述情况稍加变化,即Result.m文件中没有对应的callback函数,此时直接将需要传递的数据通过参数传递到Result.fig内:
function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) str1 = get(handles.edit1,'string'); str2 = get(handles.edit2,'string'); resultStr = strcat('Welcome,', str1, ' ', str2); handles.resultStr = resultStr;%仍然将数据保存到handles中,当数据较少时,可以不用这种方式 guidata(hObject,handles); Result(handles);%直接将数据传递到Result.fig中当我们采用这种方式直接将数据传入到Result.fig中时,matlab会利用varargin变长参数列表 保存传入的数据(varargin{1}接收传入进来的第一个数据,varargin{2}接收传递进来的第二个数据...依次类推)。然后Result_Fcn()函数会接收该varargin数据。此时我们即可以将传入的数据保存到当前figure的handles句柄中,然后该figure就可以使用传递过来的数据了。
function Result_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Result (see VARARGIN)
% Choose default command line output for Result
handles.output = hObject;
handles.varargin = varargin;%将输入的信息保存到当前figure的handles句柄中
% Update handles structure
guidata(hObject, handles);
如上图所示,[显示数据]按钮的Callback函数添加如下代码(使用传入进来的参数):
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h = handles.varargin{1,1};%使用
set(handles.edit1, 'String', h.resultStr);
对应情况(4):
这种情况比较简单,一般是通过传入“键值对”设置被传入参数figure的一些常用属性:
比如,我想利用StrCat.fig设置Result.fig的position位置大小以及resize是否可以缩放信息。那在StrCat.fig中的[组合]按钮的Callback函数中添加如下代码,即可以设置Result.fig的position和Resize信息。(注意各个Result.fig是否可以最大化的区别)
function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) str1 = get(handles.edit1,'string'); str2 = get(handles.edit2,'string'); resultStr = strcat('Welcome,', str1, ' ', str2); handles.resultStr = resultStr; guidata(hObject,handles); Result('Position', [60 60 60 10], 'Resize', 'on'); Result('Position', [60 60 60 20], 'Resize', 'off'); Result('Position', [60 60 60 30], 'Resize', 'on');