Beruflich Dokumente
Kultur Dokumente
Why use a GUI in MATLAB? The main reason GUIs are used is because it makes things simple for the end users of the program! If GUIs "ere not used# people "ould ha$e to "ork from the command line interface# "hich can be e%tremely difficult and fustrating! Imagine if you had to input te%t commands to operate your "eb bro"ser &yes# your "eb bro"ser is a GUI too'(! It "ouldn)t be $ery practical "ould it? In this tutorial# "e "ill create a simple GUI that "ill add together t"o numbers# displaying the ans"er in a designated te%t field!
This tutorial is "ritten for those "ith little or no e%perience creating a MATLAB GUI &Graphical User Interface(! Basic kno"ledge of MATLAB is not re*uired# but recommended! MATLAB $ersion +,,-a is used in "riting this tutorial! Both earlier $ersions and ne" $ersions should be compatible as "ell &as long as it isn)t too outdated(! Lets get started'
+! 0ou should see the follo"ing screen appear! 1hoose the first option Blank GUI (Default)!
2! 0ou should no" see the follo"ing screen &or something similar depending on "hat $ersion of MATLAB you are using and "hat the predesignated settings are(3
4! Before adding components blindly# it is good to ha$e a rough idea about ho" you "ant the graphical part of the GUI to look like so that it)ll be easier to lay it out! Belo" is a sample of "hat the finished GUI might look like!
Add in all these components to the GUI by clicking on the icon and placing it onto the grid! At this point# your GUI should look similar to the figure belo" 3
+! 9e%t# its time to edit the properties of these components! Let)s start "ith the static te%t! :ouble click one of the Static Text components! 0ou should see the follo"ing table
appear! It is called the Property Inspector and allo"s you to modify the properties of a component!
2! We)re interested in changing the String parameter! Go ahead and edit this te%t to +!
After modifying these properties# the component may not be fully $isible on the GUI editor! This can be fi%ed if you resi;e the component# i!e! use your mouse cursor and stretch the component to make it larger! 4! 9o"# do the same for the ne%t Static Text component# but instead of changing the String parameter to +# change it to =! <! /or the third Static Text component# change the String parameter to "hate$er you "ant as the title to your GUI! I kept it simple and named it MyAdderGUI! 0ou can also e%periment around "ith the different font options as "ell!
=! /or the final Static Text component# "e "ant to set the String 8arameter to 0! In addition# "e "ant to modify the Tag parameter for this component! The Tag parameter is basically the $ariable name of this component! Let)s call it answer stati!"e#t! This component "ill be used to display our ans"er# as you ha$e probably already ha$e guessed!
-! 6o no"# you should ha$e something that looks like the follo"ing3
+! /or the second Edit Text component# set the String parameter to 0 BUT set the Tag parameter in$ut2 edit"e#t! This component "ill store the second of t"o numbers that "ill be added together! 2! /inally# "e need to modify the pushbutton component! 1hange the String parameter to Add& and change the Tag parameter to add $us'(utton! 8ushing this button "ill display the sum of the t"o input numbers!
>earrange your components accordingly! 0ou should ha$e something like this "hen you are done3
<! 9o"# sa$e your GUI under any file name you please! I chose to name mine )yAdder! When you sa$e this file# MATLAB automatically generates t"o files3 myAdder.fig and myAdder.m! The !fig file contains the graphics of your interface! The !m file contains all the code for the GUI!
/or the purposes of this tutorial# "e are primarily concerned only "ith the callback functions! 0ou don)t ha$e to "orry about any of the other function types! .! 7pen up the !m file that "as automatically generated "hen you sa$ed your GUI! In the MATLAB editor# click on the icon# "hich "ill bring up a list of the functions "ithin the !m file! 6elect input1_editText_ allback!
fun!tion in$ut% edit"e#t ,all(a!k('-(.e!t/ e0entdata/ 'andles) 2 '-(.e!t 'andle to in$ut% edit"e#t (see G,B-) 2 e0entdata reser0ed 4 to (e defined in a future 0ersion of MA"5AB 2 'andles stru!ture wit' 'andles and user data (see GUIDA"A)
2 8int9 get('-(.e!t/:;tring:) returns !ontents of in$ut% edit"e#t as te#t <+ 2 str2dou(le(get('-(.e!t/:;tring:)) returns !ontents of %0+ 2 in$ut% edit"e#t as a dou(le
This piece of code simply makes sure that the input is "ell defined! We don)t "ant the user to put in inputs that aren)t numbers' The last line of code tells the gui to update the handles structure after the callback is complete! The handles stores all the rele$ant data related to the GUI! This topic "ill be discussed in depth in a different tutorial! /or no"# you should take it at face $alue that it)s a good idea to end each callback function "ith guidata('-(.e!t/ 'andles)= so that the handles are al"ays updated after each callback! This can sa$e you from potential headaches later on! ..! Add the same block of code to input!_editText_ allback! .+! 9o" "e need to edit the add_pushbutton_ allback! 1lick on the icon and select add_pushbutton_ allback! The follo"ing code block is "hat you should see in the !m file!
%*+ %1+ %3+ %6+ %7+ 2 444 ?#e!utes on (utton $ress in add $us'(utton+ fun!tion add $us'(utton ,all(a!k('-(.e!t/ e0entdata/ 'andles) 2 '-(.e!t 'andle to add $us'(utton (see G,B-) 2 e0entdata reser0ed 4 to (e defined in a future 0ersion of MA"5AB 2 'andles stru!ture wit' 'andles and user data (see GUIDA"A)
total = str2nu)(a) + str2nu)(()= ! = nu)2str(total)= 2 need to !on0ert t'e answer (a!k into ;tring ty$e to dis$lay it set('andles+answer stati!"e#t/:;tring:/!)= guidata('-(.e!t/ 'andles)=
.A! Let)s discuss ho" the code "e ?ust added "orks3
%<+ a = get('andles+in$ut% edit"e#t/:;tring:)= 20+ ( = get('andles+in$ut2 edit"e#t/:;tring:)=
The t"o lines of code abo$e take the strings "ithin the Edit Text components# and stores them into the $ariables a and b! 6ince they are $ariables of String type# and not "umber type# "e cannot simply add them together! Thus# "e must con$ert a and b to "umber type before MATLAB can add them together! +.! We can con$ert $ariables of String type to "umber type using the MATLAB command str2nu)(;tring argu)ent)! 6imilarly# "e can do the opposite using nu)2str(@u)(er argu)ent)! The follo"ing line of code is used to add the t"o inputs together!
22+ total= (str2nu)(a) + str2nu)(())=
The ne%t line of code con$erts the sum $ariable to String type and stores it into the $ariable c!
! = nu)2str(total)=
The reason "e con$ert the final ans"er back into String type is because the Static Text component does not display $ariables of "umber type! If you did not con$ert it back into a String type# the GUI "ould run into an error "hen it tries to display the ans"er! +2! 9o" "e ?ust need to send the sum of the t"o inputs to the ans"er bo% that "e created! This is done using the follo"ing line of code! This line of code populates the Static Text component "ith the $ariable c!
21+ set('andles+answer stati!"e#t/:;tring:/!)=
The last line of code updates the handles structures as "as pre$iously mentioned!
guidata('-(.e!t/ 'andles)=
1ongratulations# "e)re finished coding the GUI! :on)t forget to sa$e your m file! It is no" time to launch the GUI' +<! If you don)t "ant MATLAB to automatically generate all those comments for each of the callbacks# there is a "ay to disable this feature! /rom the GUI editor# go to File# then to Preferences!
The second method is to launch the GUI from the MATLAB command prompt! /irst# set the MATLAB current directory to "here$er you sa$ed your !fig and !m
file!
9e%t# type in the name of the GUI at the command prompt &you don)t need to type the !fig or !m e%tension(3
Try to input some numbers to test out the GUI! 'on rat#lations on creating your first GUI'
1+ a = get('andles+in$ut% edit"e#t/:;tring:)=