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 does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL Commands in MySQL: Practical ExamplesSQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AM

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

How does InnoDB handle ACID compliance?How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AM

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft