Last year to do some of the MATLAB GUI program, and now to do related things, in retrospect, a lot of experience was not recorded, and now look back always feel uncomfortable, so from now on, we must write records.
Let's start with a simple example.
There are usually two ways to create a Matlab GUI interface:
1. Directly add controls dynamically using. m files
2. Use Guide to quickly generate GUI interfaces
Obviously the second visual editing method is more suitable for writing large programs.
One: Create a GUI
1. Dynamically add in. m files
For example
H_main=figure (' name ', ' a demo of GUI design ', ' menubar ', ' none ',...
' Numbertitle ', ' off ', ' position ', [100 100 300 100];
H_edit=uicontrol (' style ', ' edit ', ' backgroundcolor ', [1 1 1], ' position ', [+],...
' Tag ', ' Myedit ', ' string ', ' 1 ', ' horizontalalignment ', ' left ');
H_but1=uicontrol (' style ', ' pushbutton ', ' position ', [+], ' string ', ' INC ',...
' Callback ', [' V=eval (Get (H_edit, ' string ')); ',...
' Set (H_edit, ' string ', Int2str (v+1));
H_but2=uicontrol (' style ', ' pushbutton ', ' position ', [+], ' string ', ' DEC ',...
' Callback ', [' V=eval (Get (H_edit, ' string ') '); ', ' Set (H_edit, ' string ', Int2str (V-1));
2. Using Guide to help create a GUI
Enter guide in command, either from the menu or from the shortcut button
After new and saved, the corresponding fig file and M file will be generated, and in the layout editing view, the following tools can be used:
Layouts Editor: layout editors;
Alignment tool: Alignment tools;
Property Inspector: Object attribute observer;
Object Browser: Objects browser;
Menu Editor: MenuBar.
Two: Using controls
Create a new layout (window) in which you can add the following controls
1. static text (statically text) 2. edit box (editor Text) control
3. list box (ListBox) control 4. scroll bar (slider) control
5. button (push button) control 6. Switch buttons (Toggle button) controls
7. Radio buttons (Radio button) control 8. Button Group Control
9. check box control 10. list box (ListBox) control
11. Pop-up menu (Popup menu) control 12. Axis (Axes) control
13. Panels (panel) controls
Each control has its own properties general properties are:
First, control style and appearance
(1) BackgroundColor: Sets the background color of the control, using [R G B] or color definitions.
(2) CData: A true Color image displayed on a control, using a matrix representation.
(3) Foregroundcolor: text color.
(4) String property: The text on the control, and the options for the list box and pop-up menu.
(5) Visible: whether the control is visible.
Ii. general information of the object
(1) Enable property: Indicates the enabled state of this control, set to On ", which means optional," off "is not optional.
(2) Style: The type of the control object.
(3) Tag: Control representation (user defined).
(4) Tooltipstring property: Prompt information display. When the mouse pointer is over this control, a prompt message is displayed.
(5) UserData: User-specified data.
(6) Position: The size and position of the control object.
(7) Units: Sets the position and size of the control units
(8) About font properties, such as Fontangle, FontName, etc.
Third, the execution of the control callback function
(1) Busyaction: Handles the interrupt of the callback function. There are two options, Cancel: Cancel interrupt event, queue: Queue (default setting).
(2) BUTTONDOWNFCN property: The handler function when the button is pressed.
(3) Callback attribute: It is the link of the substantive function of the whole program system of connecting program interface. The property value should be a string that can be evaluated directly, and the string will be evaluated automatically when the object is selected and changed.
(4) CREATEFCN: A callback function that executes during object generation.
(5) DELETEFCN: A callback function that executes during the deletion of an object.
(6) Interruptible property: Specifies whether the current callback function allows interrupts at execution time to perform other functions.
Iv. Control Current state information
(1) Listboxtop: The index of the topmost string displayed in the list box.
(2) Max: Maximum value.
(3) Min: Minimum value.
(4) Value: The current value of the control.
You can use the property editor to set properties
Three: Write callback function CallBack
Each control has several callback functions, and the right-click Control typically has the following menu:
Then you can jump to the corresponding editor to edit the code, guide will automatically generate the corresponding function body, function name, the name is generally control tag+ call type name parameter has three (Hobject, EventData, handles)
Where Hobject is the source control where the event occurred, EventData is the event data structure, handles is the object handle passed in
CREATEFCN occurs when a control object is created (typically initialization style, color, initial value, etc.)
DELETEFCN occurs when a real space object is cleared
BUTTONDOWNFCN and KEYPRESSFCN are mouse clicks and key events respectively callback
Callback is a generic callback function that differs depending on the control. For example, occurs when a button is pressed, a drop-down box changes value, occurs when the Sliderbar is dragged, and so on.
Four: Hierarchical relationships between handle graphs
Common functions that can create graphical handles
1. Figure function: Create a new drawing object.
2. Newplot function: Prepare to start drawing new drawing objects.
3. Axes function: Create an axis drawing object.
4. Line function: Draw lines.
5. Patch function: Fills a polygon.
6. Surface functions: Draw three-dimensional surfaces.
7. Image function: Displays the Picture object.
8. Uicontrol function: Generates a user-controlled graphical object.
9. Uimenu function: Generate a menu in the graphics window with the Hierarchy menu and the next Level submenu.
A few practical small functions:
Uigetfile Select File dialog box
Uiputfile Save File dialog box
Uisetcolor Setting the Color dialog box
Fontsetcolor Set Font dialog box
MsgBox message Box
Warndlg Warning Box
Helpdlg message box
However, if you want to make the parent window unavailable, you need to use uiwait to focus on the user dialog box.
For example:
H=helpdlg (' Please press me! ', ' Attention ');
Uiwait (h);
four: Get and set object properties
Common functions:
GCF function: Gets the handle of the current graphics window
GCA function: Gets the handle of the current axis
GCO function: Gets the handle of the current object
GCBO function: Gets the handle of the object that is currently executing the call
GCBF function: Gets the graphics handle that includes the object that is executing the call
Delete function: Delete the graphic object corresponding to the handle
Findobj function: Finding a drawing object with some property
Setup Method:
(1) The Get function returns the current value of some object properties. For example:
P=get (obj, ' Position ');
(2) The function set changes the handle graphic object properties, for example:
Set (obj, ' Position ', vect);
Five: function call
In an M file, you can define multiple functions, but the filenames must be consistent with the first function (the main function).
The other functions in this file are private functions of this file, which are not accessible externally (can be accessed by means of a parameter invocation, described below).
For example, in a program, after creating a fig, an M file is generated:
function Varargout =febirdfun (varargin)
Gui_singleton = 1;
gui_state = struct (' Gui_name ', Mfilename, ...
' Gui_singleton ', Gui_singleton, ...
' GUI_OPENINGFCN ', @febirdfun_OpeningFcn, ...
' GUI_OUTPUTFCN ', @febirdfun_OutputFcn, ...
' GUI_LAYOUTFCN ', [], ...
' Gui_callback ', []);
If Nargin && Ischar (Varargin{1})
Gui_state.gui_callback = Str2func (Varargin{1});
End
If nargout
[Varargout{1:nargout}] = GUI_MAINFCN (Gui_state, varargin{:});
Else
GUI_MAINFCN (Gui_state, varargin{:});
End
Parse this main function: First gui_state is a structure that specifies the figure open and output functions,
Starting gui_callback is empty, if the input parameter number is not 0, then the first parameter is passed to Gui_state.callback.
Next to the GUI_MAINFCN function processing, GUI_MAINFCN according to Gui_state and incoming parameters
To determine if it is a child function, or open the fig file and run OPENINGFCN and OUTPUTFCN.
If the gui_callback is empty, then run open the main window fig file, otherwise, call the child function
This function has the following functions:
1. The ability to open the fig file and initialize the control when the input has no parameters
2. When you specify a function name that needs to be called, you can use it as another control callback (but actually call a child function in the same file)
For example, in the callback of a control: (Febrdfun main function)
Febirdfun ("@push_button_Callback", gcbf,[])
Such a call can invoke the child functions inside the file.
Of course, you can use the functions in other files as callback, but in that case, data transfer can be cumbersome.
Here's a look at Figure openning Function
%================================
function MAIN_OPENINGFCN (hobject, EventData, handles, Varargin)
Handles.output = Hobject;
% typically define your own data structures in this area such as
S=struct ("P1", v1 ...
"P2", V2);
Handles. Mystruct=s;
% update handles data structure is very important!!
Guidata (hobject, handles);
%================================
function varargout = MAIN_OUTPUTFCN (Hobject, eventdata, handles)
Varargout{1} = handles.output;
This function is the definition of the output return value
Note: in MATLAB, function-corresponding end can be not, but as the version is updated, end will be required.
VI: Data transfer
The two methods that I use frequently in the MATLAB GUI program for value passing:
1. Using the handles data structure to transmit the value of the main function
For example, there is an object handles in one of the image window handles (you can define your own data structure)
Then in a sub-callback function, the data can be passed through handles:
function Sliderbar1_callback (hobject,eventdata,handles)
Percent EG1
V1=get (hobject, ' value ');
Obj=handles. Text1% Direct positioning of the TAG as a Text1 control
Set (obj, ' value ', v1);
Percent EG2
Handles. MYSTRUCT.P1=V1; % change the data in handles
Guidata (Hobject,handles); % UPDATE handles!! Important
End
2. Using the UserData of the control to pass the value
Each control typically has its own custom field, UserData, you can define a domain to hold your own data, such as in a TIMERFCN that defines a timer
T1=timer (' TIMERFCN ', {@Timer1Fcn}, ' ERRORFCN ', @Timer1ErrorFcn}, ' Period ', 1.0, ' ExecutionMode ', ' fixedrate ');
T1. Userfata=handles. MyStruct;
Here the TIMER1FCN call does not appear to pass any parameters, in fact, the definition of this function must have at least two parameters
function TIMER1FCN (obj,eventata)
Obj. userdata=xxxxx;% can share data like this
End
Of course, when the timer is defined, it is allowed to pass a parameter, which can also be used to pass values.
T1=timer (' TIMERFCN ', {@Timer1Fcn, handles}, ' ERRORFCN ', {@Timer1ErroFcnr, handles}, ' Period ', 1.0, ' ExecutionMode ', ' Fixedrate ');
Matlab GUI Interface Programming Summary