Updated March 3, 2023
Introduction to Matlab Forms
Matlab forms are used to perform some operations and store some information in a database. Basically, it is used for GUI ( graphical user interface) in Matlab to visualize implementation effectively. ‘guide’ command is used to switch or to open GUI panel. It has various blocks like a push button, edit text, axes, toggle button, push menu, and many more. These blocks used for various operations.
Syntax:
- Guide
The first syntax used to open GUI panel is ‘guide’. This command we need to compile in the command window.
- Get ( ) and Set ( )
Get and set functions are used to get the input from the user and to display the result on GUI.
How to Create form in Matlab?
There are various steps to create a form in Matlab. Let us consider one simple example of a form where we want information about the roll numbers of users. here we are accepting only one input in form of a number and we are going to display this particular block on the result side.
Step 1: The first step is use ‘guide’ command and compile it in the command window. After compiling this command we need to save the file in the appropriate folder and then we will get one GUI window.
Step 2: GUI window has various functional blocks like a push button, edit text, axes, toggle, radio button, control, and many more.
Step 3: As per the example requirement, we need one text box to display enter your roll number and one edit text box to take input from the user. then there is one button which is used for operations to get a result that is ‘push button’ so we will drag one push button on the panel. And last required block is a text box to display our result.
How to add Validation in Matlab?
There is one push button that is used in every program of GUI based applications. this button gives operational code or program in Matlab editor .there are various inbuilt function codes in Matlab editor. to access the code we need to right-click on the push-button then we will get options like create a function, delete function, keypress function, call back, etc. after getting these functions to click on call back then we will get code for a push button.
In code, we are going to use two functions get ad set .here get function is used to receive input from a used, and set function is used to set the result which is received from blocks. In this example get function will get one numeric value from the roll number but in a panel, we have inserted a text box, therefore, we need to convert the text into a numeric value. And then set function will display the roll number as it is. To convert text into numeric value one syntax is used which is ‘str 2 num’.
Example
Let us assume a simple example of the addition of two numbers. In this example two inputs are required so for two inputs two edit text blocks are required and for better visualization and understanding we will display messages to enter two numbers that is enter number one and enter number two. To add these two numbers we need one push-button .here push-button is used to do the additional operation and at last to display the result we need a text block. when we set all the text blocks tag option is very important because this tag is used in operations. By default, the tag is edit 1, if we are using multiple tags then it will be edit1, edit 2, and so on.
In Matlab code we have used three variables a, b and c. a and b are two input variables. When we enter number one then a will take the value of that number and the tag used for this variable is edit 1. When we enter number two then b will take the value of that number and the tag used for this variable is edit 3. Variable c is used for the addition of a and b, therefore set function is used to get results. Tag is used for variable c is edit 3. Str2num function is used to convert string data into numeric data or value. if we are using string data only in operation then there is no need to use this function.
Matlab Code:
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
a=str2num(get(handles.edit1,'string'));
b=str2num(get(handles.edit2,'string'));
c = a + b ;
set(handles.edit3,'string',c);
Output:
Conclusion
In this article, we have seen how to create forms in Matlab. A Matlab form is one of the examples of a graphical user interface. By using this method we can create various types of forms in Matlab. These forms include various functionalities like a drag, drop, set, enter values or text, choosing an option from a given database, bullet button, etc.
Recommended Articles
This is a guide to Matlab Forms. Here we discuss the Introduction, syntax, How to Create, and add form in Matlab along with the examples respectively. You may also have a look at the following articles to learn more –