Matlab figure passes data to figure (1)

Matlab figure passes data to figure (1)

When using Matlab to write GUI, we often encounter the problem that one figure needs calculation data in another figure, which involves the problem of data transfer between different figures. In addition to declaring global variables through global, this article introduces the use of handles to transfer figure data to another figure.

1. The call of figure

Calling a figure opens and initializes a figure. Before initialization of any figure, the following code will be executed (assuming figure is 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
The above code gives several ways to open (or call) figure:

(1) Result, directly open a new figure or make the previously opened figure window become the focus window again

(2) H = Result, directly open a new figure or make the previously opened figure window become the focus window again and return the handle of the figure

(3) Result('The name of a function in the Result.m file', hObject, eventdata, handles, ...), find out whether there is a function with the corresponding name in the Result.m file, and then use the function as the figure's Callback function and call this function, this method cannot open the figure or make the already opened figure become the focus window again. (This method can call functions inside m-file outside m-file) . In addition, these parameters will be passed to the corresponding OpeningFcn function through the varargin variable.

(4) Result('Property1', 'val1', 'Property2', 'val2', ...) directly opens a new figure or makes the previously opened figure window become the focus window again, in the OpeningFcn function corresponding to the figure Pass these "key-value pairs" into the GUI before calling. These "key-value pair" parameters modify the interface settings of the corresponding figure (the corresponding property value in the Figure's Property Inspector). In addition, these "key-value pairs" parameters will be passed to the corresponding OpeningFcn function through the varargin variable.

From the above (3) and (4), we can pass parameters into the figure through varargin, and let the figure continue to process the incoming data . So what is varargin? Then look down.

2、varargin

In matalb, varargin represents the variable-length parameter list of the function, which is generally declared at the end of the function parameter to collect the remaining input variables of the function.

For example, define function func1(x, y, varargin), and use func1(1, 2, 3, 4) to call the function, varargin{1} = 3, varargin{2} = 4; when using func1(1, 2, 3) varargin{1} = 3 when calling the function.

3. The figure data is passed into the figure

After the above introductions (3) and (4), we know that when a figure is opened, data can be passed into the figure. So how did it get in?

Corresponding situation (3):

In this case, the figure to which we passed the parameter can only call the corresponding callback function and cannot open the figure. Another thing to note is that the first parameter passed in is the name of a function, the second parameter is either empty or must be an object handle hObject or handles, and these parameters need to correspond to the parameter list of the callback function.

To illustrate the usage, as shown in the above figure, StrCat.fig combines String 1 and String 2, and then combines these two strings by clicking the [Combination] button. Under the callback function of the [combination] button, pass a combined string to Result.fig. In order to pass parameters to Result.fig, we save the combined string to the handles structure in StrCat.fig, and then pass the handles to the corresponding function. The callback function corresponding to the [Combination] button is as follows:

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles) Callback function of the [combination] button in %StrCat.fig
% 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; % save the combined string to handles
guidata(hObject,handles);

Result('dispAAA',handles); % Pass parameters to Result.fig, where dispAAA is the function under Result.fig and handles is the handle of StrCat.fig


The dispAAA() function in the Result.m file is as follows:

function dispAAA(handles)
disp('run function dispAAA()...');
disp('the reconstructed string is:');
disp(handles.resultStr); %handles is the handle of StrCat.fig, at this time the information of StrCat.fig has been passed
In this case, only the corresponding callback function can be called and Result.fig cannot be opened, so the spliced ​​string is output through the command line. The specific effect is as follows:


In fact, the above introduction is not the information transfer between figures in the true sense. Because Result.fig doesn't appear at all. The above situation can be slightly changed, that is, there is no corresponding callback function in the Result.m file. In this case, the data to be passed is directly passed to Result.fig through parameters:

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; % still save the data to handles, when there is less data, you can not use this method
guidata(hObject,handles);

Result(handles); % Pass the data directly into Result.fig
When we directly pass data into Result.fig in this way, matlab will use the varargin variable-length parameter list to save the incoming data (varargin{1} receives the first incoming data, varargin{2} receive the second data passed in...and so on) . Then the Result_Fcn() function will receive the varargin data. At this point, we can save the incoming data to the handles handle of the current figure, and then the figure can use the passed data.

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);

As shown in the figure above, add the following code to the Callback function of the [Display Data] button (using the incoming parameters):

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);

Corresponding situation (4):

This situation is relatively simple. Generally, some common attributes of the passed parameter figure are set by passing in "key-value pairs":


For example, I want to use StrCat.fig to set the position size of Result.fig and whether resize can scale the information. Then add the following code to the Callback function of the [Combination] button in StrCat.fig, that is, you can set the position and Resize information of Result.fig. (Note the difference between whether each Result.fig can be maximized)

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');





Guess you like

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