(1)数据传递
global的实例:
实现GUI中单击按钮文本中的数字增加和减少:
创建GUI,添加静态文本,两个按钮,设置Tag和其他属性:
为按钮添加回调函数:
function varargout = gui_var(varargin) % GUI_VAR MATLAB code for gui_var.fig % GUI_VAR, by itself, creates a new GUI_VAR or raises the existing % singleton*. % % H = GUI_VAR returns the handle to a new GUI_VAR or the handle to % the existing singleton*. % % GUI_VAR('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in GUI_VAR.M with the given input arguments. % % GUI_VAR('Property','Value',...) creates a new GUI_VAR or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before gui_var_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to gui_var_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 gui_var % Last Modified by GUIDE v2.5 28-Aug-2014 08:39:26 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @gui_var_OpeningFcn, ... 'gui_OutputFcn', @gui_var_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 gui_var is made visible. function gui_var_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 gui_var (see VARARGIN) % Choose default command line output for gui_var handles.output = hObject; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %声明global变量并赋初值 global count; count=0; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Update handles structure guidata(hObject, handles); % UIWAIT makes gui_var wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = gui_var_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变量,否则该变量时局部变量 global count; count=count+1;%次数增加 set(handles.number,'String',num2str(count));%设置文字,num2str将数字转换成字符串 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % --- 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 count; count=count-1; set(handles.number,'String',num2str(count)); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
运行结果:
使用UserData
UserData可以使用界面中的任何控件的该属性值,但是有n个控件,最多可以使用n个UserData来进行数据传递。
程序:(界面以及Tag和属性跟global相同)
function varargout = gui_var(varargin) % GUI_VAR MATLAB code for gui_var.fig % GUI_VAR, by itself, creates a new GUI_VAR or raises the existing % singleton*. % % H = GUI_VAR returns the handle to a new GUI_VAR or the handle to % the existing singleton*. % % GUI_VAR('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in GUI_VAR.M with the given input arguments. % % GUI_VAR('Property','Value',...) creates a new GUI_VAR or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before gui_var_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to gui_var_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 gui_var % Last Modified by GUIDE v2.5 28-Aug-2014 08:39:26 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @gui_var_OpeningFcn, ... 'gui_OutputFcn', @gui_var_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 gui_var is made visible. function gui_var_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 gui_var (see VARARGIN) % Choose default command line output for gui_var handles.output = hObject; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %使用控件的UserData来实现变量共享 set(handles.number,'UserData',0);%设置UserData属性值 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Update handles structure guidata(hObject, handles); % UIWAIT makes gui_var wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = gui_var_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) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% num=get(handles.number,'UserData');%获取UserData属性值 num=num+1;%加一 set(handles.number,'UserData',num);%设置UserData属性值 set(handles.number,'String',num2str(num));%设置文本 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % --- 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) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% num=get(handles.number,'UserData'); num=num-1; set(handles.number,'UserData',num); set(handles.number,'String',num2str(num)); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
结果:
使用AppData
可以设置多个变量,使用setappdata和getappdata来设置或者获取AppData
程序:
function varargout = gui_var(varargin) % GUI_VAR MATLAB code for gui_var.fig % GUI_VAR, by itself, creates a new GUI_VAR or raises the existing % singleton*. % % H = GUI_VAR returns the handle to a new GUI_VAR or the handle to % the existing singleton*. % % GUI_VAR('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in GUI_VAR.M with the given input arguments. % % GUI_VAR('Property','Value',...) creates a new GUI_VAR or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before gui_var_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to gui_var_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 gui_var % Last Modified by GUIDE v2.5 28-Aug-2014 08:39:26 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @gui_var_OpeningFcn, ... 'gui_OutputFcn', @gui_var_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 gui_var is made visible. function gui_var_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 gui_var (see VARARGIN) % Choose default command line output for gui_var handles.output = hObject; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %使用AppData来实现变量共享 setappdata(handles.figure1,'mycount',0);%设置AppData属性值 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Update handles structure guidata(hObject, handles); % UIWAIT makes gui_var wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = gui_var_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) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% num=getappdata(handles.figure1,'mycount');%获取AppData属性值 num=num+1;%加一 setappdata(handles.figure1,'mycount',num);%设置AppData属性值 set(handles.number,'String',num2str(num));%设置文本 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % --- 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) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% num=getappdata(handles.figure1,'mycount');%获取AppData属性值 num=num-1; setappdata(handles.figure1,'mycount',num);%设置AppData属性值 set(handles.number,'String',num2str(num));%设置文本 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
结果同上。
使用GUI数据,向handles结构体中添加变量。
程序:
function varargout = gui_var(varargin) % GUI_VAR MATLAB code for gui_var.fig % GUI_VAR, by itself, creates a new GUI_VAR or raises the existing % singleton*. % % H = GUI_VAR returns the handle to a new GUI_VAR or the handle to % the existing singleton*. % % GUI_VAR('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in GUI_VAR.M with the given input arguments. % % GUI_VAR('Property','Value',...) creates a new GUI_VAR or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before gui_var_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to gui_var_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 gui_var % Last Modified by GUIDE v2.5 28-Aug-2014 08:39:26 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @gui_var_OpeningFcn, ... 'gui_OutputFcn', @gui_var_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 gui_var is made visible. function gui_var_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 gui_var (see VARARGIN) % Choose default command line output for gui_var handles.output = hObject; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %使用GUI数据来实现变量共享 handles.mycount=0; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Update handles structure guidata(hObject, handles); % UIWAIT makes gui_var wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = gui_var_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) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% num=handles.mycount;%获取GUI属性值 num=num+1;%加一 handles.mycount=num;%设置GUI属性值 guidata(hObject, handles);%更新GUI数据 set(handles.number,'String',num2str(num));%设置文本 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % --- 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) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% num=handles.mycount;%获取GUI属性值 num=num-1; handles.mycount=num;%设置GUI属性值 guidata(hObject, handles);%更新GUI数据 set(handles.number,'String',num2str(num));%设置文本 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
结果同上。
时间: 2024-09-29 04:32:15