Matlab figure传入数据到figure(二)
在上一篇文章中讲解了传递数据到figure的几种方式,现在补充一种方式:利用varargout将figure的OutPut_Fcn(hObject, eventdata, handles)的返回值返回给某个figure,从而实现figure传入数据到另一个figure。
1、varargout
类似于varargin,varargout表示可变长度的返回参数列表。假设定义以下返回矩阵x行数和列数的函数:
function [s,varargout] = mysize(x) nout = max(nargout,1)-1; s = size(x); for i=1:nout, varargout(i) = {s(i)}; end当调用[s,rows,cols] = mysize(rand(4,5))时,s = [4,5],varargout{1} = rows = 4,varargout{2} = cols = 5。
2、figure的OutPut_Fcn函数
假设当前的fiugre为Result.fig。该函数主要用于返回你用命令或者函数形式调用该figure时的返回值。该值在默认的情况下,一般会返回figure的句柄,然后外部程序就可以通过该句柄获取figure上各个控件的值等信息。
% --- Outputs from this function are returned to the command line. function varargout = Result_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output;%默认状态下返回该figure的句柄
该函数一般在以下三种情况下调用(当前figure为Result,其Tag也为Result):
- h = Result();直接调用figure返回其对应的句柄时
- uiresume(handles.Result);与uiwait(handles.Result)对应,当重新绘制figure时
- close(handles.Result);关闭该figure时
3、利用varargout传递数据
如前面第1节所述,varargout能够保存变长的返回参数列表,因此如果需要返回多个参数时,在对应figure下的OutPut_Fcn函数下写入:
varargout{1} = ***;varargout{2} = ***;varargout{3} = ***。注意:这里可以传递的数据可以自己按需编程序。
为了说明具体使用方法,本文对上一篇文章中的例子稍作修改:StrCat.fig输入两个字符串,然后将这两个字符串传递给Result.fig,然后Result.fig对这两字符串进行拼接处理后,先显示在Result.fig自己的面板上,然后再通过单击[回传数据]将拼接后的字符串利用本篇文章所述的方法返回给StrCat.fig。
为了能够更加清晰地演示 回传 的效果,在Result.fig的OpeningFcn函数中添加代码:uiwait(handles.Result);%让Result.fig先显示并且执行相应的操作后再调用OutPut_Fcn函数返回合并后的字符串。并在[回传数据]按钮单击的callback函数中添加uiresume(handles.Result);%让Result.fig继续显示并调用OutPut_Fcn返回对应的拼接后的字符串。
Result.m代码如下:
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 21:28:29 % 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 % --- Executes just before Result is made visible. 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); % UIWAIT makes Result wait for user response (see UIRESUME) uiwait(handles.Result); % --- Outputs from this function are returned to the command line. function varargout = Result_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; function edit1_Callback(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit1 as text % str2double(get(hObject,'String')) returns contents of edit1 as a double % --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in pushbutton2. 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}; handles.resultStr = strcat('welcome,', h.str1, h.str2); guidata(hObject, handles); set(handles.edit1, 'String', handles.resultStr); % --- Executes on button press in pushbutton3. function pushbutton3_Callback(hObject, eventdata, handles) % hObject handle to pushbutton3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.output = handles.resultStr; guidata(hObject, handles); uiresume(handles.Result);StrCat.m的代码如下:
function varargout = StrCat(varargin) % STRCAT MATLAB code for StrCat.fig % STRCAT, by itself, creates a new STRCAT or raises the existing % singleton*. % % H = STRCAT returns the handle to a new STRCAT or the handle to % the existing singleton*. % % STRCAT('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in STRCAT.M with the given input arguments. % % STRCAT('Property','Value',...) creates a new STRCAT or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before StrCat_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to StrCat_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 StrCat % Last Modified by GUIDE v2.5 12-Jun-2017 15:43:33 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @StrCat_OpeningFcn, ... 'gui_OutputFcn', @StrCat_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 % --- Executes just before StrCat is made visible. function StrCat_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 StrCat (see VARARGIN) % Choose default command line output for StrCat handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes StrCat wait for user response (see UIRESUME) % uiwait(handles.StrCat); % --- Outputs from this function are returned to the command line. function varargout = StrCat_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; function edit1_Callback(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit1 as text % str2double(get(hObject,'String')) returns contents of edit1 as a double % --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end function edit2_Callback(hObject, eventdata, handles) % hObject handle to edit2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit2 as text % str2double(get(hObject,'String')) returns contents of edit2 as a double % --- Executes during object creation, after setting all properties. function edit2_CreateFcn(hObject, eventdata, handles) % hObject handle to edit2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in pushbutton1. 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'); handles.str1 = str1; handles.str2 = str2; guidata(hObject,handles); result = Result(handles);%组合字符串的操作留给Result.fig来,然后让该fig返回组合后的结果 set(handles.text5, 'string', result);