Thursday 1 November 2012

SIMPLE CALCULATOR IN GUI MAT LAB

Here we learn to make a simple calculator in GUI function of MAT LAB. Follow all the steps :-



Note :- 
              You should have the MAT LAB version 7.0 or above. Here I use MAT LAB version 11.

Read the full post and follow the step.

1.  Open the MAT LAB. Go to File>>New>>GUI.



2.  Now a window opens like below. Select blank GUI(default) and click OK.


3.  Now a blank GUI figure opens.


4.  Here we use 17 push buttons and 1 static text editor. So place these components from the list at the left side. Push button is given as OK in the list and static text is given as TXT in the list. Arrange them in a proper order of your choice. Change their string names by double clicking on them individually one by one. You can also choose the color of static text box.


5.  Save this file. After saving a window opens which is a script file and it contains functions of all the buttons that are used in GUI figure. That window looks like below.....



6.  Scroll down above script file and look for functions of push buttons as given below...


This function is given for every button as you scroll down the page.

7.  Now we write definition of function of each button... as described below...... paste this code below the function declaration as shown in image below.....


8.  Here is code given for each button...
  • for button 1(it prints 1)

------------------------------------------------------------------------------------------------------------

global jj
textString = get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)
   set(handles.text1,'String','1') ;
else
textString =strcat(textString,'1');
set(handles.text1,'String',textString)

end
jj=0;
------------------------------------------------------------------------------------------------------------
  • for button 2(it prints 2)

------------------------------------------------------------------------------------------------------------

global jj
textString = get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)
   set(handles.text1,'String','2') ;
else
textString =strcat(textString,'2');
set(handles.text1,'String',textString)

end
jj=0;
------------------------------------------------------------------------------------------------------------

  • for button 3(it prints 3)

------------------------------------------------------------------------------------------------------------

global jj
textString = get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)
   set(handles.text1,'String','3') ;
else
textString =strcat(textString,'3');
set(handles.text1,'String',textString)

end
jj=0;
------------------------------------------------------------------------------------------------------------
  • for button 4(it prints 4 )

------------------------------------------------------------------------------------------------------------

global jj
textString = get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)
   set(handles.text1,'String','4') ;
else
textString =strcat(textString,'4');
set(handles.text1,'String',textString)

end
jj=0;
------------------------------------------------------------------------------------------------------------
  • for button 5(it prints 5)

------------------------------------------------------------------------------------------------------------

global jj
textString = get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)
   set(handles.text1,'String','5') ;
else
textString =strcat(textString,'5');
set(handles.text1,'String',textString)

end
jj=0;
------------------------------------------------------------------------------------------------------------
  • for button 6(it prints 6)

------------------------------------------------------------------------------------------------------------

global jj
textString = get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)
   set(handles.text1,'String','6') ;
else
textString =strcat(textString,'6');
set(handles.text1,'String',textString)

end
jj=0;
------------------------------------------------------------------------------------------------------------
  • for button 7(it prints 7)

------------------------------------------------------------------------------------------------------------

global jj
textString = get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)
   set(handles.text1,'String','7') ;
else
textString =strcat(textString,'7');
set(handles.text1,'String',textString)

end
jj=0;
------------------------------------------------------------------------------------------------------------
  • for button 8(it prints 8)

------------------------------------------------------------------------------------------------------------

global jj
textString = get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)
   set(handles.text1,'String','8') ;
else
textString =strcat(textString,'8');
set(handles.text1,'String',textString)

end
jj=0;
------------------------------------------------------------------------------------------------------------
  • for button 9(it prints 9)

------------------------------------------------------------------------------------------------------------

global jj
textString = get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)
   set(handles.text1,'String','9') ;
else
textString =strcat(textString,'9');
set(handles.text1,'String',textString)

end
jj=0;
------------------------------------------------------------------------------------------------------------
  • for button 10(it prints 0)

------------------------------------------------------------------------------------------------------------

global jj
textString = get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)
   set(handles.text1,'String','0') ;
else
textString =strcat(textString,'0');
set(handles.text1,'String',textString)

end
jj=0;
------------------------------------------------------------------------------------------------------------
  • for button 11(it prints 00)

------------------------------------------------------------------------------------------------------------

global jj
textString = get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)
   set(handles.text1,'String','00') ;
else
textString =strcat(textString,'00');
set(handles.text1,'String',textString)

end
jj=0;
------------------------------------------------------------------------------------------------------------
  • for button 12(it prints +)

------------------------------------------------------------------------------------------------------------

textString = get(handles.text1,'String');
textString =strcat(textString,'+');
set(handles.text1,'String',textString)

------------------------------------------------------------------------------------------------------------

  • for button 13(it prints -)

------------------------------------------------------------------------------------------------------------

textString = get(handles.text1,'String');
textString =strcat(textString,'-');
set(handles.text1,'String',textString)

------------------------------------------------------------------------------------------------------------

  • for button 14(it prints *)

------------------------------------------------------------------------------------------------------------

textString = get(handles.text1,'String');
textString =strcat(textString,'*');
set(handles.text1,'String',textString)

------------------------------------------------------------------------------------------------------------

  • for button 15(it prints /)

------------------------------------------------------------------------------------------------------------

textString = get(handles.text1,'String');
textString =strcat(textString,'/');
set(handles.text1,'String',textString)

------------------------------------------------------------------------------------------------------------
  • for button 16 for equal to or for evaluate the answer(for executing the input numbers)
------------------------------------------------------------------------------------------------------------
textString = get(handles.text1,'String');
ans =eval(textString);
set(handles.text1,'String',ans)
------------------------------------------------------------------------------------------------------------
  • for button 17(it clears the display)
------------------------------------------------------------------------------------------------------------
set(handles.text1,'String','0.') ;
------------------------------------------------------------------------------------------------------------

9.  After these steps press run from the GUI figure window. If it asked for the change path then click on the change folder. A new window opens and your calculator is ready to run.



IF YOU HAVE ANY QUERY THAN CONTACT US OR GIVE YOUR QUERY IN THE QUERY OPTION.      
                                                            GOOD LUCK

More embedded projects >>....                             More MAT LAB projects>>...