Monday, 25 November 2013

How to use Pushbutton in MATLAB GUI?

AIM: Study and Implementation of Pushbutton in MATLAB GUI.
1. For opening GUI tool simply type following command in command window.
>> guide
 This command will open GUI toolbox.

Fig: 1 Starting menu of GUI

Next step is to click on ‘Blank GUI (Default)’ and click on OK tab. This process will open another window with a lot of buttons and various menus. By using that buttons and menus user can create useful, effective GUI.  

Fig: 2 GUI menu bar with all Buttons
2. In second step simply click over pushbutton as mentioned in fig 2. Drag that 
button in a empty space as mentioned in fig 3.

Fig: 3 GUI with a pushbutton

3. Next step is to change the properties of the pushbutton. For this just double click on pushbutton this action will open property inspector. Property inspector consists of various properties of the pushbutton.

Fig: 4 Before property changes

Fig: 5 After property changes
4. Take one static text box in a GUI window. 
Fig: 6 figure with a static text box

5. Similarly as we did for Pushbutton double click on static text and convert background color, font size, font weight, convert tag or remember original tag as it is. Make string of static text empty because we want to display something on static text.  After conversion our fig file looks like following fig.7 

Fig: 7 After property conversion of Static text box

6. Now just save this ‘fig file’ with any name in a folder. Remember one thing name of the fig file should be continuous. You have to save that file without any extension. After saving this file MATLAB will immediately open ‘m file’ as shown in following fig. 8
Fig: 8 ‘m file of respective project’

7. Now next thing is to linking of the button with a static text box. Our aim is to display text ‘ON’ after clicking on the pushbutton. So for making this possible you have to right click on pushbutton and select ‘view callback à callback’ as shown in following fig. 9

Fig: 9 Callback viewing

8. It will directly link in ‘m file’ as shown in following fig. 10

Fig: 10 After clicking on Callback

9. Now here is main part i.e main linking code. We want to write our code after third line of the status callback as shown in following fig. 11
For getting status of the button you have to use ‘get’ command.
Syntax for get:
Var = get(handles.tag_of_the_pushbutton,’value’);
For setting value to static text you have to use ‘set’ command.
Syntax for set:
Var = set(handles.tag_of_the_static_text,’string’,’message’ or value);


Fig: 11 Linking code of the Pushbutton

10. Next step is to save this ‘m file’ and simply run the code. Following figure will shows you how output window looks like.

Fig: 12 Before clicking status button
Fig: 13 After clicking status button

No comments:

Post a Comment