Matlab gui, 2. Use MATLAB functions to implement matlab gui, part1, and diagram

Source: Internet
Author: User
Tags uicontrol
1. In fact, the GUI of MATLAB can be fully implemented using the functions of various controls in MATLAB. It can also be said that GUI is a figure, and I am also a learner interested in MATLAB GUI, in the course of learning MATLAB, I searched a lot of materials to understand how to use the function form, that is, to do not rely on the GUIDE to complete the GUI Design. Here I will summarize and share it.

1. In fact, the GUI of MATLAB can be fully implemented using the functions of various controls in MATLAB. It can also be said that GUI is a figure, and I am also a learner interested in MATLAB GUI, in the course of learning MATLAB, I searched a lot of materials to understand how to use the function form, that is, to do not rely on the GUIDE to complete the GUI Design. Here I will summarize and share it.

I. Overview

In fact, the GUI of MATLAB can be fully implemented using the functions of various controls in MATLAB. It can also be said that GUI is a figure, and I am also a learner interested in MATLAB GUI, in the course of learning MATLAB, I searched a lot of materials to understand how to use the function form, that is, the GUI Design method is not based on the GUIDE. I will summarize and share it here. If there is any error, I also hope that some netizens will point out that.

Matlab gui, 2 this part of content in the form of functions to implement a simple jump rope game, including a simple start interface, and death interface, very simple, it does not involve any difficult algorithms. It is just a reference for implementing the function of the matlab gui. This article is the first part and mainly introduces some controls to be used.

2. Image (figure)

I will not talk much about the image. I will mainly introduce the image handle.

Figure_handler = figure; % You should try to add a number after a returned statement to improve the running speed.

The above Code creates a default image and returns the image handle to handler. To create a figure, you only need to change the handler attribute value.

When creating a figure, you can change the attribute value of the figure, for example,

Figure_handler = figure ('name', 'test', 'numbertitle', 'off ');

After the image is created using the preceding statement, the digital title is disabled and the image name is set to test, the newly created image will only display test in the title bar.

The figure attributes required in this article are as follows:

WindowKeyPressFcn: press the callback function on the keyboard of the image window. This function is called when the image window is set to the current window and the key on the keyboard is pressed.

Name: image name, window name, and string value

Units: the unit of measure of location and other information in the image. The common values are 'normalized' and 'pixel)

Position: the position of the button in the image, which is a four-dimensional vector. [left start point, bottom start point, width, and height]

Numbertitle: the title of the number displayed in the window. It is useless. It is set to 'off' or 'on'

Resize: identify whether the window can be redefined by the user, set to 'off' or 'on'

Menubar: the menu bar of the image. It can be a menu bar handle, or 'None' or 'true'. 'none' indicates no menu bar. 'figure 'indicates the menu bar of the default drawing window.

Toolbar: the toolbar of the image. It can be a toolbar handle, or 'None' or 'true'. 'none' indicates that there is no toolbar, and 'figure 'indicates the toolbar of the default drawing window.

Three set and get Functions

If you need to use the matlab gui, set and get functions are the most basic. The set function is used to set the handle property value. The prototype is as follows:

Set (figure_handler, 'property1', value1, 'property2', value2)

The get function is used to obtain the property value of the handle. The prototype is as follows:

Value = ge (figure_handler, 'properties ');

If you want to change the mouse of the previously created image, you only need to execute the following statement:

Set (figure_handler, 'pointer ', 'cross ');

Now, move the mouse over the image you just created and observe the mouse. You will find a different one. If you want to know more types, find figure properties in the help document.

For more information about the properties of set, get, and figure, see the http://blog.csdn.net/szv123_rier/article/details/8157218.

4. Create a button on the Image

We need to use the pushbutton button to complete some interactions. The button is created by using the function uicontrol. The call method is as follows.

Button_handler = uicontrol (figure_handler, 'style', 'pushutton ');

Fiugre_handler is used as the button's parent handle. The handle of this button is the image's sub-handle. 'Style' defines the style. Here it is 'pushutton '. Other attributes can be defined at the time of creation. The attribute values of buttons used in this article are as follows,

Units: the unit of measure of location and other information in the image. The common values are 'normalized' and 'pixel)

Position: the position of the button in the image, which is a four-dimensional vector. [left start point, bottom start point, width, and height]

String: the string displayed on the button.

Fontsize: There are characters, of course, there is a font size.

Callback: the callback function, which is executed when the button is pressed.

Five axis (axes)

The coordinate axis is the control for displaying images. The plot function of MATLAB is completed on axes. That is to say, the whole game is drawn here.

The creation method is as follows,

Axes_handler = axes;

You can also change other attributes when creating a project. The attribute values required in this document are as follows,

Units, position: Same as above

Ylim: 2-dimensional vector, [Y-axis minimum, Y-axis maximum], marking the y-axis display range.

Xlim: Same as ylim, but this is about the X axis.

Tickdir: tick is the mark pointer in the coordinate axis, that is, the dial line on the axis. tickdir is set to the direction of the dial line. It is optional for 'out' and 'in', indicating that it points to the inside or outside the coordinate axis.

Summary

This section briefly introduces how to create images, buttons, coordinate axes, and some attributes, as well as set and get functions for Operation attributes. Complete a simple example before the end to consolidate it.

Fh = figure ('name', 'part1demo',... set the image name
'UNIT ', 'pixel',... sets the unit. The unit is the same as the button. Set it to pixel here to adjust the position.
'Position', [800,600,],... suppose the computer is 1280X768 resolution, then I set a x image, try to be in the center of the screen
'Numbertitle', 'off',... close the digital title of figure.
'SIZE', 'off',... the window size conversion is not supported.
'Menubar ', 'none',... cancel the default menu bar
'Toolbar', 'none'... Cancel default toolbar
);
Ah = axes ('Units ', 'normalized',... sets the unit and sets it to normalization,
'Position', [,],... overlay the entire window
'Tickdir', 'out',... sets the direction of the axis mark to outward, so that the axis won't be seen.
'Xlim', [1,800],... set the X axis range to 1 to 800.
'Ylim', [1,600]... set the Y axis to 1 to 600.
);
Bh = uicontrol (fh,... parent handle is the image handle just created
'Style', 'pushutton ',... the style is a button.

'String', 'draw a sine wave-',... set button text

'Fontsize', 40,... set the font size

'Units ', 'normalized',... sets the unit to normalization.
'Position', [0.2, 1,],... the setting button is under the window.
'Callback', 'plot (sin (1: 800) '... sets the callback function of the button to draw a sine wave in the coordinate axis.
);

Oops. In this example, you forgot to add set and get. But it's okay. If you want to change its attributes after the creation, you can use set, so you won't write more.

Laizhang, before clicking the button

Click the button

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.