Saturday, 1 September 2012

MATLAB GUI Basic Programming:Tutorial 1-Simple Button Click Event Example

Hello Code Breakers,
Today I am going to start new tutorial series of MATLAB GUI programming especially for my fellow juniors who are making their project in MATLAB.

Today I will show you one simple tutorial on how to use button click event. But before that just keep one thing in mind about programming.

Your Programming will be as great as your Imagination. 

The more you imagine well the more great your programming will be. There is no limit until we make one. So just keep your hard work and your great imagination UP and you will find your way in every complex and situation, and i will be sure that you will be amazed by your own programming and will be asking yourself
"Woww... Is this software made by me???" :-D

Now enough of this Philosophy...Lets Break It.

So i assume that you have well installed MATLAB in your computer.so here we go,open MATLAB and you will see following screen.

 
Now follow the Step 1 and Step 2 as show in above figure.

When You completed with Step 2 the GUIDE Quick Start window will be appeared.It has some predefined templates. Select Blank GUI(Default) and press Ok.


Now when you click ok, You will see a GUIDE window.


So first of all save this file as your desired location. Trust me friends it is good habit to save file from the starting of work. :-D



Here i am saving this file as Tutorial1. Now friends note here when you save this file for first time, two file will be generated with ".m" and ".fig" extension. For me it will be "Tutorial1.m" and "Tutorial1.fig" .

Now in GUIDE you will see toolbar at the left side.


Now as you can see in above figure that i have point out specific 3 components. Push Button,Static Text and Edit Text.

Push Button: It is general button.
Edit Text: It is a text box in witch we can write text.
Static Text: It is a general text which act as Label.

Now one by one drag and drop all 3 components in working area.



Now we will change the properties of every component. we will see the example of "Edit Text" properties and you have to do same for rest of the components.

So keep the cursor on Edit Text and press Right Click.and select "Property Inspector".



You will see Inspector Window.




Now we want edit text Box empty so for that find "String" property from Inspector window and delete whatever is written in its value.Here you also change "Tag"  value according to your wish. This Tag will be used as unique identifier for your particular component.  Here i hav ket that as it is.

Do the same for "Static Text" and "Push Button".



here for Push Button I have Change its String to "Click Me"  and its Tag to "btn1". Now you will see the screen like this.


Now Save this file.


Now Switch to MATLAB main screen when you will see the Tutorial1.m file is already open in Editor.


Now find below function in that file.


function btn1_Callback(hObject, eventdata, handles)
% hObject    handle to btn1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




This function is Callback function For btn1(Push Button) component. When the Button will click this function will be executed. So here now what we want to do is When we click on Button, what ever we have written in Edit Box will be Printed on Static Text. Like this.





So for doing that we need to add two line of code at the end of Callback function of btn1 Push Button.


% --- Executes on button press in btn1.
function btn1_Callback(hObject, eventdata, handles)
% hObject    handle to btn1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=get(handles.edit1,'String');%This line will fetch the data from Edit Text Box
set(handles.text1,'String',data);%This line will printed the fetch data on Static Text.



Here get() function is used to fetch data from Edit Text whose Tag is edit1. And set() function is used to print data on Static Text whose Tag is text1. As you have might notice that what is "data". data is variable witch is used to store the value fetched from edit1(Edit Box). 

So thats it. Save the file and Run it. You will see the Output.

Hope you have find this Tutorial Helpful, Though it is very basic step but i find this necessary so that even a person first time using MATLAB can also understand it.

Note: Please Do comment if you have any suggestion about upcoming tutorials. Also want to request any specific case you can do that. More tutorial are coming soon. Till then....

Break It, Feel It And Learn It.
 

3 comments:

  1. Jambore Casino and Resort - Jammyhub
    The 강릉 출장마사지 Jambore Casino 충청남도 출장안마 and 정읍 출장안마 Resort features everything you need to experience this exciting 군산 출장안마 casino destination in your home town. Jambore Casino 청주 출장마사지 and Resort

    ReplyDelete