This document allows you to learn the process of making Matlab GUI through a simple example. First, the graphical user interface Gui (graphical user Interfaces) is a tool and method for information exchange between users and computers, consisting of a variety of graphical objects. In this user interface, the user's command and control of the program is through the mouse and other input devices "Select" a variety of graphic objects to achieve. Developers can complete the design of the application simply by adding their own operation or control code to the program code that is automatically generated by the Software development tool.
Steps:
1. Create a new GUI file: Select Blankgui (Default)
2. After entering the GUI development environment, add two Edit text boxes, 6 static text boxes, and a button, arranged as shown in the following figure; (+ = data 1 green box, etc. are static text boxes )
After you have laid out the controls, you can write programs for these controls to implement the two-number additions.
3. Add code to the data 1 text box
Click on the red box shown above and select Edit1_callback, and the cursor will immediately move to the location of the code below.
function Edit1_callback (Hobject,eventdata, handles)
% Hobject handle to Edit1 (SEEGCBO)
% eventdata reserved-to bedefined in a future version of MATLAB
% handles structure Withhandles and user data (see GUIDATA)
% Hints:get (hobject, ' String ') returns contents of Edit1 as text
% str2double (Get (hobject, ' String ')) returns contents of Edit1 as a double
Then insert the following code below this code:
% stores the contents of the Data text box 1 as a string. If the string is not a number, the actual blank content
Input =str2num (Get (hobject, ' String '));
% check whether the input is empty. If blank, the default display is 0
if (IsEmpty (input))
Set (Hobject, ' String ', ' 0 ')
End
Guidata (Hobject,handles);
This code makes the input strictly limited and we cannot attempt to enter a non-numeric one.
4. Add the same piece of code for Edit2_callback
5. Add code for the calculation button to achieve the purpose of adding data 1 and Data 2.
Use the same method in 3 to find the Pushbutton1_callback code snippet in the M file as follows;
Functionpushbutton1_callback (Hobject, eventdata, handles)
% Hobjecthandle to Pushbutton1 (see GCBO)
% eventdatareserved-to BES defined in a future version of MATLAB
% handles Structurewith handles and user data (see GUIDATA)
Add the following code after the above code;
A = Get (handles.edit1, ' String ');
b = Get (Handles.edit2, ' String ');
% A and B is variables ofstrings type, and need to be converted
% to variables of number Typebefore they can be added together
Total = Str2Num (a) +str2num (b);
c = Num2str (total);
% need to convert the Answerback into String type to display it
Set (Handles.text1, ' String ', c); % Text1 is the tag of the static text box (the last green box that displays the result)
Guidata (Hobject,handles);
Program Analysis:
A = Get (handles.edit1, ' string '); b = Get (Handles.edit2, ' string ');
The above line of code to the user input data into the variable a, variable B;
% B is a character variable, and you need to convert it to a digital type before calculating the two.
Total = Str2Num (a) +str2num (b);
This code allows two numbers to be added
c = Num2str (total);
Set (Handles.text1, ' String ', c);
Guidata (Hobject,handles);
The above two lines of code are used to update the calculation result text box and the graphic object handle, the general callback callback function is Guidata (hobject, handles); End to update the data