MATLAB GUI ,2,使用MATLAB的函数来实现MATLAB GUI,part 3,全
一 全局变量 这里介绍全局变量主要是为了完成参数的传递,其实参数的传递方法还有很多,比如说setappdata和getappdata,或者使用guidata,guidata这个就不推荐了,再或者是用对象上的userdata,等等。本人较推荐setappdata和getappdata,关于setappdat和geta
一 全局变量
这里介绍全局变量主要是为了完成参数的传递,其实参数的传递方法还有很多,比如说setappdata和getappdata,或者使用guidata,guidata这个就不推荐了,再或者是用对象上的userdata,等等。本人较推荐setappdata和getappdata,关于setappdat和getappdat可以参照本人写的另外一篇http://blog.csdn.net/davied9/article/details/7738984。介绍全局变量也是为了尝试用另一种方式完成参数传递,而且全局变量和其他几种方法的最大区别是,不用操心存储它的问题,其他几种方式在变量值修改后需要手动存储一次。
全局变量的使用非常简单,只需在使用前用global声明即可。
global handles;
这里声明了一个handles全局变量,使用时直接调用即可。但需要注意的是,handles变量一直存在,而在声明了全局变量的函数里,使用该名称的变量均为全局变量,所以在使用前和不再使用时,养成一个好习惯,将全局变量赋值为空。
handles = [];
二 计时器
计时器是MATLAB的timer对象,主要用于计时 - - ,创建方式如下,
timer_handler = timer;
同样,这是一个默认的timer,在建立时也可以设置其属性。现在我们就可以通过timer来完成对图像的更新。
关于timer,详细的最好doc timer一下,不仅有timer的属性,可以取的值,我们需要的属性如下,
BusyMode :取值为'drop','error','queue',为MATLAB忙时,回调函数的处理方式,'drop'为丢弃,'error'为调用timer的errorfcn函数,'queue'为排队处理。
ExecutionMode :取值为'singleShot','fixedDelay','fixedRate','fixedSpacing'。'singleShot'仅执行一次,后面三个有区别,但都是固定延时,区别见doc的图,贴在下面了。
Period :就是执行固定时延的周期,单位是秒。
TimerFcn :回调函数,完成定时调用的重要属性了。
InstantPeriod :本次调用timer和上次timer之间的即时周期。虽然设定了timer的周期,但是当我们将busymode设为'queue'时,此属性就非常重要了,是作为程序运行时间的一个参考和修正。
taskstoexecute :指明计时器调用次数
三 状态机
不讲理论!!简单的说就是用一个变量来指示当前系统的工作状态。多说无益,进入小结,以实践阐述。
四 小结
这一部分介绍的和前面两个部分不一样,前两个部分主要都是关于实现方面的,而这一部分完成的是控制。下面以一个简单的例子将这三个主题结合起来。
这个例子非常非常简单,定时输出字符串,通过检测到不同的工作状态输出不同的字符串。
那么本例就以全局变量来建立一个状态机,用timer完成字符串的输出,代码如下。
function part3(in)
global th % timer 句柄
global p3_state % 状态机,这里设置的很简单,只有两个状态,0和1,分别代表初始化和结束
if nargin
eval(in) % 通过eval完成timer_callback,timer回调函数的调用
else
if ~isempty(th) % 若建立timer前th全局变量被占用,删除并置空
delete(th);
th = [];
end
% 创建timer
th = timer( ...
'busymode','queue',... % 排队模式
'timerfcn','part3(''timer_callback'')',... % 回调函数,每个周期执行一次
'period',0.02,... % 周期为0.02秒
'ExecutionMode','fixedrate', ... % 执行方式为等延时
'taskstoexecute',10 ... % 执行次数为10次
);
start(th); % 开始计时器
p3_state = 0; % 初始化状态机为'初始化'
end
function timer_callback
global th % 声明timer句柄
global p3_state % 声明状态机
if get(th,'TasksExecuted')
p3_state = 0;
else
p3_state = 1;
end
if p3_state % 根据状态不同完成输出过程
disp('结束')
else
disp('初始化')
end
运行代码,得到的结果如下,
>> part3
初始化
初始化
初始化
初始化
结束
结束
结束
结束
结束
结束
这一部分写起来很简单,但是实际操作起来还是很重要的,要慢慢的去体会

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

C++ functions play a vital role in GUI interface design, including: creating windows, setting window properties, creating controls, and handling events. Practical case: A GUI login form can be created through C++ functions, which involves steps such as creating a window, setting the window title, creating buttons and input boxes, and handling button click events.

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.
