matlib同一界面中不同控件之间的数据传递机制

(1)  使用global变量

示例代码

function varargout = global_data(varargin)
% GLOBAL_DATA MATLAB code for global_data.fig
%      GLOBAL_DATA, by itself, creates a new GLOBAL_DATA or raises the existing
%      singleton*.
%
%      H = GLOBAL_DATA returns the handle to a new GLOBAL_DATA or the handle to
%      the existing singleton*.
%
%      GLOBAL_DATA('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GLOBAL_DATA.M with the given input arguments.
%
%      GLOBAL_DATA('Property','Value',...) creates a new GLOBAL_DATA or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before global_data_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to global_data_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 global_data
 
% Last Modified by GUIDE v2.5 02-Oct-2017 14:11:32
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @global_data_OpeningFcn, ...
                   'gui_OutputFcn',  @global_data_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 global_data is made visible.
function global_data_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 global_data (see VARARGIN)
 
% Choose default command line output for global_data
handles.output = hObject;
 
global times%声明global变量
times = 0;%初始化global变量
 
% Update handles structure
guidata(hObject, handles);
 
% UIWAIT makes global_data wait for user response (see UIRESUME)
% uiwait(handles.figure1);
 
 
% --- Outputs from this function are returned to the command line.
function varargout = global_data_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;
 
 
% --- Executes on button press in add.
function add_Callback(hObject, eventdata, handles)
% hObject    handle to add (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global times%在函数内部这样声明表示使用global变量
times = times + 1;
%更新静态文本框中的值,其中disp是静态文本框的tag属性的值
set(handles.disp,'String',num2str(times));
 
% --- Executes on button press in minus.
function minus_Callback(hObject, eventdata, handles)
% hObject    handle to minus (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global times%在函数内部这样声明表示使用global变量
times = times - 1;
%更新静态文本框中的值,其中disp是静态文本框的tag属性的值
set(handles.disp,'String',num2str(times));

执行效果


方法总结:使用global变量实现同一窗口中数据传递与C和C++语言中全局变量和局部变量的方式差不多,在全局位置定义以后,可以在函数内部调用,这就是使用global变量实现同一窗口中数据传递的实质。


(2)  使用UserData属性

示例代码

function varargout = userdata_data(varargin)
% USERDATA_DATA MATLAB code for userdata_data.fig
%      USERDATA_DATA, by itself, creates a new USERDATA_DATA or raises the existing
%      singleton*.
%
%      H = USERDATA_DATA returns the handle to a new USERDATA_DATA or the handle to
%      the existing singleton*.
%
%      USERDATA_DATA('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in USERDATA_DATA.M with the given input arguments.
%
%      USERDATA_DATA('Property','Value',...) creates a new USERDATA_DATA or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before userdata_data_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to userdata_data_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 userdata_data
 
% Last Modified by GUIDE v2.5 02-Oct-2017 14:17:13
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @userdata_data_OpeningFcn, ...
                   'gui_OutputFcn',  @userdata_data_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 userdata_data is made visible.
function userdata_data_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 userdata_data (see VARARGIN)
 
% Choose default command line output for userdata_data
handles.output = hObject;
%设置pushbutton1的UserData属性的值是0
set(handles.pushbutton1,'UserData',0);
 
% Update handles structure
guidata(hObject, handles);
 
% UIWAIT makes userdata_data wait for user response (see UIRESUME)
% uiwait(handles.figure1);
 
 
% --- Outputs from this function are returned to the command line.
function varargout = userdata_data_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;
 
 
% --- 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)
%获取pushbutton1的UserData属性的值
times = get(handles.pushbutton1,'UserData');
times = times + 1;
%更新pushbutton1的UserData属性的值
set(handles.pushbutton1,'UserData',times);
%更新静态文本框的值
set(handles.text2,'String',num2str(times));
 
% --- 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)
%获取pushbutton1的UserData属性的值
times = get(handles.pushbutton1,'UserData');
times = times - 1;
%更新pushbutton1的UserData属性的值
set(handles.pushbutton1,'UserData',times);
%更新静态文本框的值
set(handles.text2,'String',num2str(times));
执行效果


方法总结:使用UserData属性进行同一窗口中不同控件之间的数据传递的实质是,借助一个中间变量UserData完成数据传递。


(3)  使用AppData

示例代码

function varargout = appdata_data(varargin)
% APPDATA_DATA MATLAB code for appdata_data.fig
%      APPDATA_DATA, by itself, creates a new APPDATA_DATA or raises the existing
%      singleton*.
%
%      H = APPDATA_DATA returns the handle to a new APPDATA_DATA or the handle to
%      the existing singleton*.
%
%      APPDATA_DATA('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in APPDATA_DATA.M with the given input arguments.
%
%      APPDATA_DATA('Property','Value',...) creates a new APPDATA_DATA or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before appdata_data_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to appdata_data_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 appdata_data
 
% Last Modified by GUIDE v2.5 02-Oct-2017 14:29:34
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @appdata_data_OpeningFcn, ...
                   'gui_OutputFcn',  @appdata_data_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 appdata_data is made visible.
function appdata_data_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 appdata_data (see VARARGIN)
 
% Choose default command line output for appdata_data
handles.output = hObject;
%设置appdata的初始变量为0 
setappdata(handles.figure1,'times',0);
 
 
% Update handles structure
guidata(hObject, handles);
 
% UIWAIT makes appdata_data wait for user response (see UIRESUME)
% uiwait(handles.figure1);
 
 
% --- Outputs from this function are returned to the command line.
function varargout = appdata_data_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;
 
 
% --- 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)
%获取times变量的值
times = getappdata(handles.figure1,'times');
times = times + 1;
%更新times变量的值
setappdata(handles.figure1,'times',times);
%更新静态文本框中的值
set(handles.text2,'String',num2str(times));
 
% --- 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)
%获取times变量的值
times = getappdata(handles.figure1,'times');
times = times + 1;
%更新times变量的值
setappdata(handles.figure1,'times',times);
%更新静态文本框中的值
set(handles.text2,'String',num2str(times));
执行效果


方法小结:使用appdata方式实现同一窗口中不同控件之间的数据传递是通过setappdata和getappdata实现变量的设置和获取,从而实现不同的控件之间的数据传递。


(4)使用GUI数据

示例代码

function varargout = guidata_data(varargin)
% GUIDATA_DATA MATLAB code for guidata_data.fig
%      GUIDATA_DATA, by itself, creates a new GUIDATA_DATA or raises the existing
%      singleton*.
%
%      H = GUIDATA_DATA returns the handle to a new GUIDATA_DATA or the handle to
%      the existing singleton*.
%
%      GUIDATA_DATA('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUIDATA_DATA.M with the given input arguments.
%
%      GUIDATA_DATA('Property','Value',...) creates a new GUIDATA_DATA or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before guidata_data_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to guidata_data_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 guidata_data
 
% Last Modified by GUIDE v2.5 02-Oct-2017 15:01:16
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @guidata_data_OpeningFcn, ...
                   'gui_OutputFcn',  @guidata_data_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 guidata_data is made visible.
function guidata_data_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 guidata_data (see VARARGIN)
 
% Choose default command line output for guidata_data
handles.output = hObject;
%初始化handles的times属性的值是0
handles.times = 0;
 
% Update handles structure
guidata(hObject, handles);
 
% UIWAIT makes guidata_data wait for user response (see UIRESUME)
% uiwait(handles.figure1);
 
 
% --- Outputs from this function are returned to the command line.
function varargout = guidata_data_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;
 
 
% --- 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)
%通过handles结构体访问times属性并更新times
handles.times = handles.times + 1;
%更新handles结构体
guidata(hObject, handles);
%更新静态文本框的值
set(handles.text2,'String',num2str(handles.times));
 
% --- 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)
%通过handles结构体访问times属性并更新times
handles.times = handles.times - 1;
%更新handles结构体
guidata(hObject, handles);
%更新静态文本框的值
set(handles.text2,'String',num2str(handles.times));
执行效果


方法总结:使用gui数据实现同一窗体不同的控件之间的数据传递,实质是借助handles结构体完成数据传递。



备注:程序源代码来自21世纪电子论坛matlib教学视频


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值