You are on page 1of 17

Hng Dn Lp Trnh GUIS Matlab Bi 1: Lp trnh giao din GUI trong Matlab (Phn 1) Mnh vit bi ny tng hp li cc hng

g dn v thit k giao din GUI trong Matlab cc bn d dng thc hin. Phn mm: Matlab R2007a Bi u tin mnh s gii thiu cc bn giao din GUI. 1. M phn mm Matlab, g lnh sau vo ca s Command >> guide Trong ca s GUIDE Quick Start c nhiu la chn theo mt trong cc khun mu sau: Create New GUI : To mt hp thoi GUI mi theo mt trong cc lo i sau Blank GUI (Default): Hp thoi GUI trng khng c mt iu khin uicontrol no c. GUI with Uicontrols: Hp thoi GUI vi mt vi uicontrol nh button, ... Chng trnh c th chy ngay. GUI with Axes and Menu: Hp thoi GUI vi mt uicontrol axes v button, cc menu hin th th. Modal Question Dialog: Hp thoi t cu hi Yes, No. Open Existing GUI: m mt project c sn. Trong hng dn ny, cc bn to mt project mi nn s chn Blank GUI 2. Ca s GUI hin ra Giao din rt ging vi cc chng trnh lp trnh giao din nh Visual Basic, Visual C++, ... Cc bn di chut qua cc biu tng bn tri s thy tn ca cc iu khin. Xin ni qua mt vi iu khin hay dng: Push Button: ging nh nt Command Button trong VB. L cc nt bm nh nt OK, Cancel m ta vn b m. Slider : Thanh trt c mt con trt chy trn . Radio Button : Nt nh hnh trn chn la Check Box Edit Text Static Text Pop-up Menu List Box Axes Panel Button Group ActiveX Control Toggle Button Cc bn hy th gp th vi iu khin vo trong giao din bn phi xem hnh dng th no ?? Hnh d ng ra sao nh ?? Cn menu th quan trng nht l menu Tools c: Run (Ctr + T) : nhn vo chy chng trnh m ta vit. C li l hin ra ngy Align Object: dng lm cho cc iu khin sp xp gn p theo mnh nh cng cn l bn tri, ... Grid and Rulers : dng cu hnh v li trong giao din v n s coi giao din nh mt ma trn

cc vung nh, ta s thay i gi tr ny cho cc iu khin c th th u ty cho p. Menu Editor : trnh ny to menu cho iu khin Tab Order Editor : sp xp Tab order l th t khi ta nhn phm Tab Gui Options : la chn cho giao din GUI. Cc bn nn vo menu Help xem hng dn thm trong Help. Cc bn save di tn: TUT01, khi ng thi xut hin ca s Editor v ang m file TUT01.m ca bn. Trong th mc bn save s c 2 file l: TUT01.fig : file ny cha giao din ca chng trnh TUT01.m : file cha cc m thc thi cho chng trnh nh cc hm khi to, cc hm callback,... (s ni chi tit vo bi khc). 3. Ko th cc iu khin Ni hi nhiu nn cc bn ght ri, nn hy lm mt v d n gi n cho vui cn hc tip no. Hy ko vo trong giao din 2 edit box, 1 static box v 1 Push Button. Chng trnh c chc nng khi nhn vo nt bm th kt qu ca php tnh cng gia 2 s c g vo 2 s hin ln trong Static Text. 4. Thay i cc thuc tnh ca cc iu khin Click p vo Edit Text bn tri xut hin ca s cc thuc tnh ca iu khin. C th sp xp theo chc nng hoc theo th t A-Z ca tn thuc tnh bng nt hin g bn tri. Cc thuc tnh ny cc bn hy th thay i xem n th no nh ?? Thuc tnh quan trng ca Edit Box bao gm: Tag: y l thuc tnh ging nh Caption trong Visual Basic t tn iu khin. Dng tn ny c th thao tc n cc thuc tnh ca i tng. Mnh t tn l: editStr1. String : l xu k t hin ln Edit Box. Cc bn xa ci ny i. Tng t, thay i thuc tnh tag ca Edit Box th 2 thnh editStr2. Static Box cng tng t thnh staticStr3. Push Button: thuc tnh tag = buttonCalculate, string = calculate 5. Vit lnh cho chng trnh Chng trnh c tc dng khi nh n vo nt Push Button s hin ln kt qu Static Box. V th nn s phi vit vo hm no m khi nh n vo Push Button s gi. Chnh l hm Callback. iu khin no cng c hm callback, nh hm ngt trong vi iu khin v y . Click chut phi vo nt Calculate chn Callback. Trong ny cn mt s hm na s tnh sau. Nhn vo nh ngha ca hm trong Editor bn s thy l: hm ny c thc hin khi nhn vo nt buttonCalculate. Hm c mt s tham s: hObject : handle ca iu khin buttonCalculate eventdata handles : l mt cu trc cha tt c cc iu khin v d liu ngi dng. Dng ci ny truy sut cc iu khin khc. Qua thuc tnh tag ca cc iu khin ta s truy sut n thuc tnh string ca cc iu khin editStr1, editStr2,editStr3 bng lnh get v set. get(handles.tag_dieu_khien, 'ten thuoc tinh'); set(handles.tag_dieu_khien, 'ten_thuoc_tinh', gia_tri); v hm quan trng na bin t string sang s l hm: str2num v num2str bin tr li. Vy chng ta s vit hm nh sau:

Nhn nt Run kim tra kt qu xem no: Tham kho Bi 2: Cc tnh cht ca cc iu khin trong GUIDE Matlab Cho cc bn. Mnh xin gi cc bn ti liu Matlab 7 - Creating Graphical User Interfaces, l ti liu dng .pdf ca ti liu trong Help ca Matlab. Cc bn no c th t c hiu c English th ti liu ny l qu y ri. Cc bi vit ca mnh da mt phn vo ti liu ny v da vo kinh nghim khi mnh lp trnh. Mnh ang lm n tt nghip nn mi ngy c gng vit mt t cho cc bn c nh. M n ca mnh lm vi FPGA, ch dng System Generator l lin quan n Matlab thi :d. Trong bi vit ny, mnh gii thiu cc bn cc tnh cht c trng ca cc iu khin m cc bn hay gp. hin ca s cc tnh cht Property Inspector ca mt iu khin chng ta c 3 cch sau: Nhn p chut vo mi iu khin. Chn iu khin ri vo menu View, chn Property Inspector. Chn iu khin ri nhn vo biu tng Property Inspector, gn ch M-file editor. Khi , ca s Property Inspector s hin ra. Khi nhn vo cc iu khin khc th ca s ny s hin thng tin tng ng cho iu khin . Mt vi tnh cht chung ca cc iu khin m cc bn nn ch : Tnh cht (property) Gi tr (value) Miu t Enable on, inactive, off Mc nh l: on Xc nh khi no th iu khin hin th ln giao din. t = off, th iu khin s khng xut hin. Max V hng. Mc nh l 1. Gi tr ln nht, ty thuc vo tng iu khin. Min V hng. Mc nh l 0 Gi tr nh nht, ty thuc vo tng iu khin. Position Vector gm 4 phn t (left, bottom, width, height) Kch thc ca iu khin v v tr tng i ca n vi iu khin cha n. String Units n v o lng dng trong xc nh v tr. Value V hng hoc vector Gi tr ca component, ty thuc vo tng component. Ngoi ra, thuc tnh cc k quan trng m cc bn phi thay ngay t u l: tag. Thuc tnh ny ging thuc tnh Caption m cc bn gp trong Visual Basic, chnh l tn phn bit gia cc iukhin.Mi iu khin ch c mt tn duy nht. cc bn nn qui nh cho d nh nh. Mnh ngh l nn kt hp gia tn ca loi iu khin v mc ch ca iu khin . V d: mt nhp d liu gi tr tin, thuc Edit Box nn mnh t tag l: editMoney. By gi i vo tng iu khin c th. 1. Push Button Thay i ch hin th (label) hin th trn iu khin ny bng cch thay i thuc tnh String. Ch hin

th trn Button ch c th l 1 dng, nn nu bn g nhiu dng trong thuc tnh String th ch hin th dng u tin. Nu s k t dng u tin ln hn b rng c th hin th ch c ca b mt Button th Matlab t rt ngn String vi du 3 chm (...).Thay i v tr ca PushButton = thay i thuc tnh Position. C th code trong MFile (thay i trong qu trnh thc thi) hoc thay i ngay lc thit k(gp th). thm mt nh vo PushButton th chng ta gn thuc tnh CData bng mt ma trn m x n x 3 ca gi tr RGB. Thc hin trong MFile hm Open ca iu khin ngay khi ch y chng trnh load nh ny vo ri. img = rand(16,64.3); set(handles.pushbutton1, 'CData',img); Ch : C th to biu tng ring ca bn cho cc nut Push Button bng cch dng Icon Editor, sau dng hm ind2rgb chuyn sang nh gn vo thuc tnh CData. Cc s kin xy ra khi nhn Push Button c vit trong cc hm ngt nh Callback,.. 2. Slider (Thanh trt) Thay i kho ng gi tr ca Slider bng cch thay i thuc tnh Min v Max. Tt nhin Min < Max. Gi tr hin ti ca Slider c = gi tr ca thuc tnh Value. Nn khi set hoc get th ta ly gi tr ny. Khi click vo 2 ci mi tn 2 bn th thanh trt s trt tng ng v 2 pha theo mt bc no . Thay i thuc tnh SliderStep = [min_step, max_step]. min_step l gi tr bc nhy khi click vo 2 mi tn. Cn Max_step l gi tr khi m click vo trong vng trt. 3. Radio Button bit nt Radio c c nh du hay l khng th chng ta xem thuc tnh Value ca n. Value = 1 th check Value = 0 th l k check 4. Check Box Check Box th thuc tnh quan trng cng tng t nh Radio Button. 5. Edit Text Thuc tnh quan trng l String, chnh l xu k t hin th trn Edit Text. Ch : hin th nh mun th cc bn cn phi xem k thm cc hm x l string nh. hin th Edit Text dng Multi-line th cn thay i thuc tnh Max, Min. Max> Min. V d: Max = 2, Min = 0 s hin th 2 dng . 6. Static Text Thng ny tng t nh Edit Text. 7. Popup Menu 8. List Box 9. Toggle Button Hic hic, vit di qu mi tay ht. Rnh ri vit cp nht thm vi ci, cn khng th cc bn thc hnh trong cc bi khc nh Bi 3 : Tng quan v hm Callback trong lp trnh GUI Sau khi cc bn to ci giao din xong th n ph n lp trnh rt quan trng, chnh l lp trnh cc hnh vi ca cc iu khin p ng li cc s kin nh nhn phm, ko thanh trt, khi chn menu, ... chnh l cc hm Callback (ging nh cc hm s kin trong Visual Basic). Th no l hm Callback ? Callback l mt hm m cc bn vit miu t hnh vi ca mt thnh phn GUI xc nh hoc l ca chnh GUI figure, iu khin cc hnh vi ca chng bng cch thc hin mt s hnh ng c vit trong hm, p ng li mt s kin ca chnh thnh phn . Cch lp trnh ny thng gi l : Lp trnh li

s kin (event driven programming). V d, khi bn nh n mt Button th v th ng khng ?? Vy th khi nhn phm th hin nhin gi hm Callback nhn phm ca Button , v trong hm Callback ny c lnh v th ri. Cc loi hm Callback ? Mi thnh phn c nhiu hm Callback khc nhau, cc bn xem thm Properties nh. Sau y lit k cc loi hm Callback v cc iu khin c th c hm ny. Callback property S kin xy ra Thnh phn c hm ny ButtonDownFcn Thc hin khi ngi dng nhn chut ln hoc trong 5 pixels ca component hoc figure. Nu l component th thuc tnh Enable phi on ( tt nhin ri ). Axes,figure,buttongroup,panel,userinterfacecontrols Callback Hnh ng ca cc component, v d nh thc thi khi ngi dng click ln Push Button hoc chn mt thnh ph n menu.Contextmenu,menu,userinterfacecontrols CloseRequestFcn Thc thi trc khi figure ng. Figure CreateFcn To cc thnh phn.N c dng khi to cc thnh phn khi n c to ra. N thc thi sau khi thnh phn hoc figure c to, nhng trc khi hin th ln trn giao din ngi dng. Axes,figure,buttongroup,contextmenu,menu,panel,userinterfacecontrols DeleteFcn Xa thnh phn. N c th c dng thc hin hnh ng xa b trc khi component hoc figure b hy b. Axes,figure,buttongroup,contextmenu,menu,panel,userinterfacecontrols KeyPressFcn Thc thi khi ngi dng nhn mt phm trong keyboard v component hoc figure ca hm callback ang c focus. Figure,userinterfacecontrols KeyReleaseFcn Thc thi khi ngi dng nh mt phm ang bm v figure vn ang c focus. Figure ResizeFcn Thc thi khi ngi dng thay i kch thc ca panel, button group, hoc figure vi Buttongroup,figure,paneliu kin thuc tnh Resize ca figure = on. SelectiononChangeFcn Thc thi khi ngi dng la chn mt nt Radio Button khc hoc toggle button khc trong thnh phn Button Group. Buttongroup WindowButtonDownFcn Thc thi khi bn nhn chut (tri hoc phi) trong khi con tr vn n m trong vng ca s figure. Figure WindowButtonMotionFcn Thc thi khi bn di chuyn con tr trong vng ca s figure. FigureWindowButtonUpFcn Ban u bn nhn chut (tri, hoc phi) th khi nh phm ra th hm ny s c gi. Figure WindowScrollWheelFcn Thc thi khi nt cun ca chut cun trong khi figure vn trong tm focus. Figure Bi 4: To file .exe trong Matlab dng Matlab Compiler C nhiu bn kh bn khon v vic dch mt ng dng trong Matlab ra file .exe chy c lp trn cc my khng ci Matlab ng khng ?? Hay l cch ng gi sn ph m ca mnh th nn lm th no ? c th dch ra file .exe yu cu bn phi c toolbox MATLAB Compiler. Cc bn c th xem chi tit v toolbox ny v cch dng n ti y. Mnh ngh l n hng dn qu y v Matlab Compiler,

cc bn chu kh c t u n cui l c. Cc bn ci b Help cho Matlab c th tm Toolbox ny trong phn Help (nhn F1). Trong bi vit ny, mnh xin gii thiu cch to file .exe bng Deployment Tool. Cng c giao din thn thin, khng phi g lnh, ch gp th l c. Yu cu Matlab 7.0 tr ln (v mnh test t 7.0 ). Bc 1: Chn cng c bin dch C Note: Bc ny ch cn thc hin 1 ln duy nht ban u, nu mun chn cng c bin dch khc th ch y li bc ny. >> mbuild -setup Please choose your compiler for building standalone MATLAB applications: Would you like mbuild to locate installed compilers [y]/n? y Select a compiler: [1] Lcc-win32 C 2.4.1 in C:\PROGRA~1\MATLAB\R2007b\sys\lcc [2] Microsoft Visual C++ 2005 in C:\Program Files\Microsoft Visual Studio 8 [0] None Compiler: 1 Please verify your choices: Compiler: Lcc-win32 C 2.4.1 Location: C:\PROGRA~1\MATLAB\R2007b\sys\lcc Are these correct?([y]/n): y Trying to update options file: C:\Documents and Settings\Ngo Hai Bac\Application Data\MathWorks\MATLAB\R2007b\compopts.bat From template: C:\PROGRA~1\MATLAB\R2007b\bin\win32\mbuildopts\lcccompp.bat Done . . . >> Nh cc bn thy chng ta ch cn chn cng c bin dch C c sn ca Matlab l c ri (La chn 1). Bc 2: Chy cng c Development Tool G lnh: >> deploytool Khi , giao din ca chng trnh Deployment Tool hin ra: Hnh 1: Giao din chng trnh Deployment Tool T ca s ca chng trnh cc bn c th lm theo hng d n ca chng trnh c ri. Vo menu File chn New Deployment Project hoc kich vo icon trn thanh cng c. Hnh 2: New Deployment Tool T y cc bn c nhiu la chn cho vic dch project ca bn khng nhng dch ra file .exe m cn nhiu loi file khc nh to th vin C, C++, th vin nhng vo Excel, ... Nhng y mnh chnStandalone Application to ng dng c lp. Chn ni lu project trong Location v nhn OK. Hnh 3: Ca s Deployment Tool sau khi to mi Project Sau cc bn vo menu Project -> Add file thm file ca d n cc bn vo. Lu : c th kch phi vo tng loi nh Main Function, other files,.. v chn Add File. Note: Phi thm ht cc file trong d n m bn mun dch vo trong Project va to. Bc 3: Dch chng trnh Nu mun dch th cc bn chn Tools -> Build (Ctr + B) dch. Cc bn phi kin nhn ch vi project ln nh.Nu mun ng gi sn phn th chn Tools-> Package (Ctr + P) dch. Note: Th l cc bn dch xong ri . Tuy nhin chy dc trn my khc cha xong u. Cc bn cn phi copy file MCRInstaller.exe ca phin bn Matlab ca bn ang s dng cho vo cng th mc vi file ng gi va to ra. Khi mang sang my khc th ch y file .exe n s t ng ci MCR ln u. Cn ln sau k phi ci na. bit v tr ca MCRInstaller: >> mcrinstaller The WIN32 MCR Installer, version 7.7, is: C:\Program

Files\MATLAB\R2007b\toolbox\compiler\deploy\win32\MCRInstaller.exe MCR installers for other platforms are located in: C:\Program Files\MATLAB\R2007b\toolbox\compiler\deploy\<ARCH> <ARCH> is the value of COMPUTER('arch') on the target machine. bit version ca MCR hin ti g: >> [mcrmajor,mcrminor]=mcrversion mcrmajor = 7 mcrminor = 7 Ch : Copy MCRInstaller cng vi th mc cc bn file package. Khi , chy file package th n s t ci MCRInstaller. Mnh bin dch v chy thnh cng Project RS232 Communication chy c lp trn my khng ci Matlab. Rt nhiu bn khi lp trnh GUI trong Matlab gp rc ri vi ting vit, sau mt thi gian my m mnh tm ra c 1 cch nh sau: - Chnh bng m TCVN3(ABC) trong Unikey - Trong Matlab phn Font Name g .VnTime - M word chn font .VnTime, g ni dung cn hin th - Copy vo phn Title trong Matlab --> Th l ok-->Tht n gin Lp trnh giao din trong Matlab Mc ch: To mt chng trnh thn thin vi ngi dng , gip th hin trc quan cc kt qu cn thit v trnh phi g cc cu lnh di,phc tp. Khi to GUIDE 1.M Matlab, i n ca s dng lnh v g lnh: guide 2.Chn Bl nk GUI 3. Mt mn hnh nh sau s xut hin: 4. Chng ta s add cc thnh phn bn pha tay tri c 1 giao din nh sau: To giao dinCc bn ko th cc thnh phn sau: -2 edit Text component -3 Static Text component -1 Component Pushbutton Nhp p vo mi component thit lp tn v cc thuc tnh khc.Chng ta quan tm n 2 thuc tnh c bn: String(Nhn ca component),Tag(tn ca component) Component Static Text Component Edit Text Tng t vi component Edit Text th 2 Component pushbutton Sau save file giao din li ,lc ny Matlab s to ra cho bn 1 file .m tng ng x l cc s kin do user tc ng n mi component. Vit code cho cc hm Callback ca GUI Nh ni trn, sau khi to xong v save file giao din l i, Matlab s sinh ra mt file .m tng ng x l cc s kin trn mi component. Cc hm x l s kin ny ta gi l callback function. 1. M file .m tng ng ca chng trinh trong Matlab Editor, nhn vo biu tng , s cho ra 1 danh sch cc hm callback. Chng ta tm n dng input1_editText_Callback 2. Con tr s a bn n hm tng ngfunction input1_editText_Callback(hObject, eventdata, handles) % hObject handle to input1_editText (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA) % Hint: get(hObject,'String') returns contents of input1_editText as text % str2double(get(hObject,'String')) returns contents of % input1_editText as a double Thm on code sau vo di cng ca hm %store the contents of input1_editText as a string. if the string %is not a number then input will be empty input = str2num(get(hObject,'String')); %checks to see if input is empty. if so, default input1_editText to zero if (isempty(input)) set(hObject,'String','0') end guidata(hObject, handles); hObject y thc cht l i tng edit, cu lnh th nht s ly gi tr string ca i tng input1_editText v chuyn n sang d ng s. Cu lnh if kim tra xem gi tr ca input c ph hp khng,ngha l c phi l s khng, nu khng phi s gn cho n gi tr mc nh l 0. Cu lnh cui cng update li i tung hObject sau mi ln gi hm Callback.Chng ta lm tng t vi input2_editText_callback 3. Tip n chng ta tm n hm add_pushbutton_Callback v x l Thm on code sau vo : a = get(handles.input1_editText,'String'); b = get(handles.input2_editText,'String'); % a and b are variables of Strings type, and need to be converted % to variables of Number type before they can be added together total = str2num(a) + str2num(b); c = num2str(total); % need to convert the answer back into String type to display it set(handles.answer_staticText,'String',c); guidata(hObject, handles); Gii thch 1 cht v on code: 2 cu lnh u tin l y cc gi tr 2 editText.Handles l 1 i tng ca ca s chnh, n cha tt c cc component con khc n m trong ca s ny. V vy, khi mun ly ID ca i tng input_editText ta phi tham chiu n chng thng qua i tng handle. Cc gi tr ly c cn phi chuyn sang dng s bng cu lnh str2num. Sau in kt qu ra Static Text, ta dng lnh set. Cui cng l update li giao din bng cu lnh guidata. KHi ng GUI C 2 cch chy chng trnh: 1. Cch th nht, nhn nt trong GUIDE editor. 2. Cch th 2: Thit lp Matlab current directory v ng ng dn c cha cc file ngun ca chng trnh. Sau g lnh: myAdder(trong trng hp cc file ca chong trnh l myAdder.fig v myAdder.m) 3. Chng trnh s lm vic bnh thng nu tt c cc bc trn c hon tt Cch s dng Pop-up menus component Trong bi ny bn s hc cch to v s dng thnh phn Pop-up Menu. Pop-up Menu c s dng nh

1 control thay i s la chn,n tng t nh combobox component trong cc cng c lp trnh visual ca ngn ng Basic,C ,.NET.... Sau y s trnh by 1 v d v cch s dng Popup Menus iu khin font size ca 1 dng ch. Tt nht bn nn thc hnh trn bn Matlab version 2007a. Cc phin bn c hn cng c th lm vic tt. u tin,m Matlab, i n dng lnh v g guide bn s thy hnh sau y. Chn option u tin blank GUI(theo mc nh) Nhn chut v thm thnh phn Static Text ti GUI. Tip , ko th thnh phn Pop-up menus ti GUI. Nhp p vo thnh phn Static Text thay i cc thuc tnh ca n.Thay i thuc tnh String thnh "Testing",v thay i thuc tnh Tag thnh testing_staticText nh hnh di y: Tip theo, chng ta hy chnh sa cc thuc tnh ca thnh ph n Pop-up Menus. Nhp chut vo biu tng sau hin th ca s thuc tnh ca Pop-up Menus. sau khi nhp chut vo icon, hy in cc thng tin nh sau: mc nh thuc tnh Tag l popupmenu1.Bn c th thay i tn ca n nu mun. y l nhng g chng ta c c sau nhng bc trn:. Lu GUI ca bn vi 1 ci tn mong mun. Vit code cho GUI Matlab t ng sinh ra 1 file .m cng vi file giao din v t trong cng 1 th mch.File .m l ni chng ta x l cc s kin gn vi mi thnh phn trn giao din.Trong ph m vi bi vit, chng ta tm thi ch quan tm n cc hm callback, ngha l cc hm c gi ra khi chng ta tc ng ln 1 component no . M file .m c sinh ra t ng khi bn lu file giao din GUI. Trong Matlab Editor, nhp vo biu tng ,s thy 1 danh sch cc hm callback. Chn popupmenu1_Callback. thm on code sau ti hm: %gets the selected option switch get(handles.popupmenu1,'Value') case 1 set(handles.testing_staticText,'FontSize',8); case 2 set(handles.testing_staticText,'FontSize',10); case 3 set(handles.testing_staticText,'FontSize',12); case 4 set(handles.testing_staticText,'FontSize',14); case 5 set(handles.testing_staticText,'FontSize',16); otherwise end Hy nhn qua v phn tch code 1 cht. Dng di y ly gi tr option m user chon. Nh rng khi thit k giao din lc ban u, chng ta ch nh 5 gi tr font size khc nhau cho component Popupmenus, tng ng 5 ty chn. V d user chn font size l 8 (tng ng vi v tr u tin ca Pop-up menus), dng code s tr v gi tr 1. Nu user chn s 10, ga tr tr v s l 2,... get(handles.popupmenu1,'Value') Ph thuc vo gi tr c chn, font ca Static Text component s thay i tung ng bng cch s

dng dng code sau: %where ## is the appropiate fontsize value set(handles.testing_staticText,'FontSize',##); Lu m-file! Chy v kim tra GUI Tip tc v th vi cc font size khc nhau. Nu mi th u ng th bn s thy dng ch thay i vi font size tng ng. To menu n gin vi Matlab to mt menu trong matlab, vi cc ty chn cho mi trng hp, c th lm n gin nh sau: To mt file .m vi tn gi TestMenu.m, sau vit lnh nh sau: K=menu('Choose a color','Red','Green','Blue'); switch (K) case 1 msgbox('Red') case 2 msgbox('Green') case 3 msgbox('Blue') end ca s command ca Matlab, g lnh TestMenu. Vi mi ty chn s hin ra mt hp thoi thng bo tng ng. Ch : thay i menu trn ca s figure(g lnh figure ) , ta c th dng lnh uimenu. Lnh ny s b sung thm cc menu ring vo menu chnh. f = uimenu('Label','Workspace');%to Menu vi tiu Workspace %To menu con vi tiu New Figure, c hm callback l figure, khi click vo menuitem ny,l nh figure s c thc hin, khi s hin ra 1 ca s mi. uimenu(f,'Label','New Figure','Callback','figure'); %To menu con Save, c hm thc hin l save uimenu(f,'Label','Save','Callback','save'); %To menu con Quit, c hm thc hin l exit(thot khi chng trnh) uimenu(f,'Label','Quit','Callback','exit',... %du th hin cu lnh c chia lm 2dng 'Separator','on','Accelerator','Q'); %Phm tt l Q c thit lp cho submenu Bin dch chng trnh vit bng Matlab thnh file .exe Lp trnh giao din GUI trong Matlab (Phn 1) Mnh vit bi ny tng hp li cc hng dn v thit k giao din GUI trong Matlab cc bn d dng thc hin.Phn mm: Matlab R2007a Bi u tin mnh s gii thiu cc bn giao din GUI. 1. M phn mm Matlab, g lnh sau vo ca s Command

>> guide

Trong ca s GUIDE Quick Start c nhiu la chn theo mt trong cc khun mu sau: Create New GUI: To mt hp thoi GUI mi theo mt trong cc loi sau

Blank GUI (Default): Hp thoi GUI trng khng c mt iu khin uicontrol no c. GUI with Uicontrols: Hp thoi GUI vi mt vi uicontrol nh button, ... Chng trnh c th chy ngay. GUI with Axes and Menu: Hp thoi GUI vi mt uicontrol axes v button, cc menu hin th th. Modal Question Dialog: Hp thoi t cu hi Yes, No.

Open Existing GUI: m mt project c sn. Trong hng dn ny, cc bn to mt project mi nn s chn Blank GUI 2. Ca s GUI hin ra

Giao din rt ging vi cc chng trnh lp trnh giao din nh Visual Basic, Visual C++, ... Cc bn di chut qua cc biu tng bn tri s thy tn ca cc iu khin. Xin ni qua mt vi iu khin hay dng:

Push Button: ging nh nt Command Button trong VB. L cc nt bm nh nt OK, Cancel m ta vn bm. Slider : Thanh trt c mt con trt chy trn . Radio Button : Nt nh hnh trn chn la Check Box Edit Text Static Text Pop-up Menu List Box Axes Panel Button Group ActiveX Control Toggle Button

Cc bn hy th gp th vi iu khin vo trong giao din bn phi xem hnh dng th no ?? Hnh dng ra sao nh ?? Cn menu th quan trng nht l menu Tools c:

Run (Ctr + T) : nhn vo chy chng trnh m ta vit. C li l hin ra ngy Align Object: dng lm cho cc iu khin sp xp gn p theo mnh nh cng cn l bn tri, ... Grid and Rulers : dng cu hnh v li trong giao din v n s coi giao din nh mt ma trn cc vung nh, ta s thay i gi tr ny cho cc iu khin c th th u ty cho p. Menu Editor : trnh ny to menu cho iu khin Tab Order Editor : sp xp Tab order l th t khi ta nhn phm Tab Gui Options : la chn cho giao din GUI.

Cc bn nn vo menu Help xem hng dn thm trong Help. Cc bn save di tn: TUT01, khi ng thi xut hin ca s Editor v ang m file TUT01.m ca bn. Trong th mc bn save s c 2 file l:

TUT01.fig : file ny cha giao din ca chng trnh TUT01.m : file cha cc m thc thi cho chng trnh nh cc hm khi to, cc hm callback,... (s ni chi tit vo bi khc).

3. Ko th cc iu khin Ni hi nhiu nn cc bn ght ri, nn hy lm mt v d n gin cho vui cn hc tip no. Hy ko vo trong giao din 2 edit box, 1 static box v 1 Push Button.

Chng trnh c chc nng khi nhn vo nt bm th kt qu ca php tnh cng gia 2 s c g vo 2 s hin ln trong Static Text. 4. Thay i cc thuc tnh ca cc iu khin Click p vo Edit Text bn tri xut hin ca s cc thuc tnh ca iu khin. C th sp xp theo chc nng hoc theo th t A-Z ca tn thuc tnh bng nt hin g bn tri.

Cc thuc tnh ny cc bn hy th thay i xem n th no nh ?? Thuc tnh quan trng ca Edit Box bao gm:

Tag: y l thuc tnh ging nh Caption trong Visual Basic t tn iu khin. Dng tn ny c th thao tc n cc thuc tnh ca i tng. Mnh t tn l: editStr1. String : l xu k t hin ln Edit Box. Cc bn xa ci ny i.

Tng t, thay i thuc tnh tag ca Edit Box th 2 thnh editStr2. Static Box cng tng t thnh staticStr3. Push Button: thuc tnh tag = buttonCalculate, string = calculate 5. Vit lnh cho chng trnh

Chng trnh c tc dng khi nhn vo nt Push Button s hin ln kt qu Static Box. V th nn s phi vit vo hm no m khi nhn vo Push Button s gi. Chnh l hm Callback. iu khin no cng c hm callback, nh hm ngt trong vi iu khin vy :D. Click chut phi vo nt Calculate chn Callback. Trong ny cn mt s hm na s t nh sau.

src="http://i280.photobucket.com/albums/kk189/ngohaibac_dot_net/Matlab_TUT/Maltab_tutorial_006.gi f" />

Nhn vo nh ngha ca hm trong Editor bn s thy l: hm ny c thc hin khi nhn vo nt buttonCalculate. Hm c mt s tham s:

hObject : handle ca iu khin buttonCalculate eventdata handles : l mt cu trc cha tt c cc iu khin v d liu ngi dng. Dng ci ny truy sut cc iu khin khc.

Qua thuc tnh tag ca cc iu khin ta s truy sut n thuc tnh string ca cc iu khin editStr1, editStr2,editStr3 bng lnh get v set. get(handles.tag_dieu_khien, 'ten thuoc tinh'); set(handles.tag_dieu_khien, 'ten_thuoc_tinh', gia_tri); v hm quan trng na bin t string sang s l hm: str2num v num2str bin tr li. Vy chng ta s vit hm nh sau:

Nhn nt Run kim tra kt qu xem no: