Matlab figure passes data to figure (2)

Matlab figure passes data to figure (2)

In the previous article , I explained several ways to pass data to the figure, and now add another way: use varargout to return the return value of the figure's OutPut_Fcn(hObject, eventdata, handles) to a figure, so as to realize the input of the figure data to another figure.

1、varargout

Similar to varargin, varargout represents a variable-length return argument list. Suppose you define the following function that returns the number of rows and columns of matrix x:

       function [s,varargout] = mysize(x)
       nout = max (nargout, 1) -1;
       s = size(x);
       for i=1:nout, varargout(i) = {s(i)}; end
When calling [s,rows,cols] = mysize(rand(4,5)), s = [4, 5], varargout{1} = rows = 4, varargout{2} = cols = 5.

2. Figure's OutPut_Fcn function

Suppose the current fiugre is Result.fig. This function is mainly used to return the return value when you call the figure in the form of a command or function. By default, this value generally returns the handle of the figure, and then external programs can obtain information such as the value of each control on the figure through the handle.

% --- 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;% returns the handle of the figure by default

This function is generally called in the following three cases (the current figure is Result, and its Tag is also Result):

  • h = Result(); When calling figure directly to return its corresponding handle
  • uiresume(handles.Result); corresponds to uiwait(handles.Result), when the figure is redrawn
  • close(handles.Result); when closing the figure

3. Use varargout to pass data

As mentioned in Section 1 above, varargout can save a variable-length return parameter list, so if you need to return multiple parameters, write it under the OutPut_Fcn function under the corresponding figure:

varargout{1} = ***;varargout{2} = ***;varargout{3} = ***. Note: The data that can be passed here can be programmed on demand.

In order to illustrate the specific usage, this article slightly modifies the example in the previous article: StrCat.fig inputs two strings, then passes these two strings to Result.fig, and then Result.fig compares the two strings After the splicing process, it is displayed on the panel of Result.fig, and then the spliced ​​string is returned to StrCat.fig by the method described in this article by clicking [Return Data].



In order to demonstrate the effect of the return more clearly, add code to the OpeningFcn function of Result.fig: uiwait(handles.Result);% Let Result.fig display and perform the corresponding operation first, and then call the OutPut_Fcn function to return the combined result. string. And add uiresume(handles.Result);% to the callback function of the [Return Data] button click to let Result.fig continue to display and call OutPut_Fcn to return the corresponding spliced ​​string.

 The code of Result.m is as follows:

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;% save the input information to the handles handle of the current figure
% 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);
The code of StrCat.m is as follows:

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);% The operation of combining strings is left to Result.fig, and then let the fig return the combined result
set(handles.text5, 'string', result);


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325726916&siteId=291194637