matlib同一界面中不同控件之间的数据传递机制
来源:互联网 发布:电脑怎么检查网络 编辑:程序博客网 时间:2024/06/13 20:57
(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 EDITgui_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_datahandles.output = hObject; global times%声明global变量times = 0;%初始化global变量 % Update handles structureguidata(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 structurevarargout{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 EDITgui_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_datahandles.output = hObject;%设置pushbutton1的UserData属性的值是0set(handles.pushbutton1,'UserData',0); % Update handles structureguidata(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 structurevarargout{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 EDITgui_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_datahandles.output = hObject;%设置appdata的初始变量为0 setappdata(handles.figure1,'times',0); % Update handles structureguidata(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 structurevarargout{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));执行效果
(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 EDITgui_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_datahandles.output = hObject;%初始化handles的times属性的值是0handles.times = 0; % Update handles structureguidata(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 structurevarargout{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属性并更新timeshandles.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属性并更新timeshandles.times = handles.times - 1;%更新handles结构体guidata(hObject, handles);%更新静态文本框的值set(handles.text2,'String',num2str(handles.times));执行效果
方法总结:使用gui数据实现同一窗体不同的控件之间的数据传递,实质是借助handles结构体完成数据传递。
备注:程序源代码来自21世纪电子论坛matlib教学视频
阅读全文
0 0
- matlib同一界面中不同控件之间的数据传递机制
- matlib不同界面之间的数据传递机制
- Matlab GUI 设计(4):不同控件之间的数据传递
- mui不同界面之间的信息传递
- C#中不同窗体之间的数据传递
- Android中不同Activity之间的数据传递
- PHP中不同页面之间的数据传递方式
- ThinkAndroid快速开发框架中界面之间数据的传递
- VC中控件和变量之间数据传递的方式
- 不同Activity之间的数据传递
- 不同Activity之间的数据传递
- 不同Activity之间的数据传递
- C#不同窗体之间的数据传递
- 不同Activity之间的数据传递
- MFC不同对话框之间的数据传递
- Android:不同Activity之间的数据传递
- Android:不同Activity之间的数据传递
- android不同进程之间的数据传递
- 解决No module named google.protobuf.internal
- (crm笔记1-2) Dao层的搭建
- 并行计算及其发展
- wc命令的C语言实现
- 记录一次问题
- matlib同一界面中不同控件之间的数据传递机制
- ubuntu常用软件
- Runtime.getRuntime().exec
- 爬虫去重策略
- Ubuntu 16.04 下配置adb工具环境变量
- 浓墨重彩之OpenStack-02-所安装服务概览
- UISearchDisplayController
- python中的注释
- bzoj 4514: [Sdoi2016]数字配对(二分图+费用最大流)