Matlab实例学习----数据传递

(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

Matlab实例学习----数据传递的相关文章

matlab callback 数据传递

M文件中内的每个Callback都可以视为一个独立的可执行的接口,因此,任一个Callback触发后所执行的运算值若要在其他Callback中使用,就无法与MATLAB工作空间内的变量继续执行操作,也就是说每个Callback之间无法直接做沟通操作,因此变量的传递会有问题.主要的几种数据传递方式主要有:(1)宣告为全局变量将欲传递的变量在传递双方的Callback位置中都设置为全局变量,就可以直接在任一方的Callback中调用.这种方式是最简单的操作方式,但是当GUI全局变量太多时,在执行上就

OpenCV和Matlab 通过XML传递数据

因为现在下到的数据集大部分都使用了Matlab的Calibration toolbox 进行标定,其结构大部分是.mat结构的,所以它和opencv中数据传递需要一个中间过程,网上也有直接调用matlab的dll的方法,但是中间件方法必然会受到版本限制,所以我最后还是选择了使用xml来作为中间文件传递数据. --------------1.calibration toolbox原始标定的.m数据--------------   http://ishare.iask.sina.com.cn/f/6

Express框架与html之间如何进行数据传递

关于Node.js 的Express框架介绍,推荐看菜鸟教程的Express框架,很适合入门,这里不再赘述,这里主要讲一下Express框架与html之间如何进行数据传递 我采用的是JQuery的Ajax()向后台传参方式 (url传参) 一.首先先讲一下jQuery的Ajax()向后台传参(参考http://blog.csdn.net/ailo555/article/details/48859425) 1.Type属性为Get时: (1)第一种方法:(通过url传参) function Get

propsData 选项 全局扩展的数据传递

propsData 不是和属性有关,他用在全局扩展时进行传递数据,结合自定义属性获取属性值的props一起使用 html <div id="app"> <register></register> </div> js var regi = Vue.extend({ template:`<div><h2>{{message}}--{{a}}</h2></div>`, data:function(

Android(java)学习笔记220:开发一个多界面的应用程序之界面间数据传递

1.界面跳转的数据传递 (1)intent.setData() --> intent.getData():     传递的数据比较简单,一般是文本类型的数据String:倘若我们传递的数据比较复杂(几种类型),或者比较长,这个方法就不怎么实用了. (2)如果有很多的不同类型的数据传递 intent.putExtra(key, value): • 基本类型的数据都可以传递,基本类型数据的数组也可以传递 • 对象必须是实现序列化接口的对象.Bitmap • 传递一组数据:          放数据:

Android页面跳转和数据传递

Android应用开发-页面跳转和数据传递 Activity Android四大组件之一 可以理解为Android中的界面,每一个界面都是一个Activity Activity的使用必须在清单文件中进行配置 在清单文件中, 创建第二个Activity 需要在清单文件中为其配置一个activity标签 标签中如果带有这个子节点,则会在系统中多创建一个快捷图标 <intent-filter> <action android:name="android.intent.action.M

无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)

1.smartimageview使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"

ASP.NET MVC3中Controller与View之间的数据传递总结

</pre>在ASP.NET MVC<span style="font-family:宋体">中,经常会在</span>Controller<span style="font-family:宋体">与</span>View<span style="font-family:宋体">之间传递数据,因此,熟练.灵活的掌握这两层之间的数据传递方法就非常重要.本文从两个方面进行探讨:&

Android数据传递方法

Android中,Activity之间的数据传递有些复杂,比如通过Intent和Bundle等等,传递复杂对象时必须是实现了Serializable接口的类,这在很多时候使得处理页面间的数据传递很不方便.在JavaWeb中,Session对象传递数据很实用方便,对此,在Android中可不可以自己实现一个类似JavaWeb中的Session呢? 其实很简单,在不严谨的情况下,具体实现过程如下: /*/////////////////////////////////////////////////