search
HomeDatabaseMysql TutorialMATLAB 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的图,贴在下面了。

MATLAB GUI ,2,使用MATLAB的函数来实现MATLAB GUI,part 3,全

    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
初始化
初始化
初始化
初始化
结束
结束
结束
结束
结束
结束

 

这一部分写起来很简单,但是实际操作起来还是很重要的,要慢慢的去体会

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQL BLOB : are there any limits?MySQL BLOB : are there any limits?May 08, 2025 am 12:22 AM

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

MySQL : What are the best tools to automate users creation?MySQL : What are the best tools to automate users creation?May 08, 2025 am 12:22 AM

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

MySQL: Can I search inside a blob?MySQL: Can I search inside a blob?May 08, 2025 am 12:20 AM

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool