Matlab gui, 2. MATLAB functions are used to implement matlab gui, part3, complete

Source: Internet
Author: User
A global variable. Here we will introduce global variables mainly to pass parameters. In fact, there are many methods to pass parameters, such as setappdata and getappdata, or using guidata and guidata, which is not recommended, or use userdata on the object. I recommend setappdata and getappdata. For more information about setappdat and geta

A global variable. Here we will introduce global variables mainly to pass parameters. In fact, there are many methods to pass parameters, such as setappdata and getappdata, or using guidata and guidata, which is not recommended, or use userdata on the object. I recommend setappdata and getappdata. For more information about setappdat and geta

A global variable

Here we will introduce global variables mainly to pass parameters. In fact, there are many methods to pass parameters, such as setappdata and getappdata, or using guidata or guidata, which is not recommended, or use userdata on the object. I recommend setappdata and getappdata. For details about setappdat and getappdat, refer to another article I wrote: http://blog.csdn.net/davied9/article/details/7738984. Global variables are introduced to try to pass parameters in another way. The biggest difference between global variables and other methods is that you don't have to worry about storing them, in other ways, you need to manually store the variable values after they are modified.

The use of global variables is very simple. You only need to declare global variables before use.

Global handles;

A handles global variable is declared here and can be called directly during use. However, the handles variable always exists. In a function that declares a global variable, all variables using this name are global variables. Therefore, before and after use, make it a good habit to assign a value to the global variable as null.

Handles = [];

Two timers

A timer is a timer object of MATLAB. It is mainly used for timing--. The creation method is as follows,

Timer_handler = timer;

Similarly, this is a default timer, and its attributes can be set at the time of creation. Now we can use timer to update the image.

For details about timer, it is best to doc timer. There are not only timer attributes, but also values that can be taken. The required attributes are as follows,

BusyMode: The value is 'drop', 'error', and 'queue '. When MATLAB is busy, 'drop' is the processing method of the callback function, 'error' is the errorfcn function that calls timer, and 'queue 'is the queue processing.

ExecutionMode: The value is 'singleshot ', 'fixeddelay', 'fixedrate', and 'fixedspacing '. 'Singleshot 'is executed only once. The next three items are different, but they are all fixed latencies. For the difference, see the doc diagram and paste it below.

Period: refers to the Period of execution of a fixed latency, in seconds.

TimerFcn: callback function, which is an important attribute of scheduled calls.

InstantPeriod: the real-time period between the current call of timer and the previous call of timer. Although the timer cycle is set, this attribute is very important when we set busymode to 'queue '. It serves as a reference and correction for running time.

Taskstoexecute: indicates the number of timer calls.

Three-state machine

No theory !! Simply put, a variable is used to indicate the operating status of the current system. It is useless to say more. Let's go to the Summary and explain it in practice.

Summary

This section is different from the previous two parts. The first two parts are mainly about implementation, and this part is about control. The following is a simple example to combine these three themes.

This example is very simple. It outputs strings at regular intervals and outputs different strings by detecting different working states.

In this example, we create a state machine with global variables and use timer to output strings. The Code is as follows.

Function part3 (in)
Global th % timer handle
Global p3_state % state machine. The setting is very simple. There are only two States: 0 and 1, indicating initialization and end, respectively.
If nargin
Eval (in) % calls the timer_callback and timer callback functions through eval
Else
If ~ Isempty (th) % if the th global variable before timer creation is occupied, delete and leave it empty
Delete (th );
Th = [];
End
% Create timer
Th = timer (...
'Busymode', 'queue ',... % queuing Mode
'Timerfcn', 'part3 (''timer _ callback'') ',... % callback function, executed once every cycle
'Period ', 0.02,... % the cycle is 0.02 seconds
'Executionmode', 'fixedrate',... % The execution method is delayed.
'Taskstoexecute ', 10... % execution times: 10
);
Start (th); % start Timer
P3_state = 0; % the initialization state machine is 'initialization'
End

Function timer_callback
Global th % declares the timer handle
Global p3_state % declares the State Machine
If get (th, 'tasksexecuted') <5% the status switching condition of the state machine set here is also very simple. You only need to run the timer five times.
P3_state = 0;
Else
P3_state = 1;
End
If p3_state % completes the output process according to different States
Disp ('terminal ')
Else
Disp ('initialization ')
End

Run the code and the result is as follows,

> Part3
Initialization
Initialization
Initialization
Initialization
End
End
End
End
End
End

This part is easy to write, but the actual operation is very important.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.