240 likes | 350 Vues
Pattern recognition lab 8. TA : Nouf Al-Harbi :: nouf200@hotmail.com. Lab objective:. Dealing with GUI Make Bayesian Classifier using GUI. Steps for making Bayesian classifier using GUI. Why use a GUI in MATLAB?. it makes things simple for the end-users of the program
E N D
Pattern recognition lab 8 TA : Nouf Al-Harbi :: nouf200@hotmail.com
Lab objective: • Dealing with GUI • Make Bayesian Classifier using GUI
Why use a GUI in MATLAB? • it makes things simple for the end-users of the program • If GUIs were not used, people would have to work from the command line interface • It can be extremely difficult and frustrating.
Steps for making Bayesian classifier using GUI • open up MATLAB • File New GUI
Final form we should have • 8 Edit Text components • 9 Static Text component • 2 Pushbutton component • 2 figures
Steps for making Bayesian classifier using GUI Add in all these components to the GUI by clicking on the icon and placing it onto the grid
Steps for making Bayesian classifier using GUI • Its time to edit the properties of these components • Double click one of the components • You can also, Right-click the component and select property inspector
Steps for making Bayesian classifier using GUI • The Tag parameter is basically the variable name of this component • The Stringparameter is the text of this component
Steps for making Bayesian classifier using GUI • Now, save your GUI under any file name • When you save this file MATLAB automatically generates two files • myClassifier.fig file • contains the graphics of your interface • myClassifier.mfile • contains all the code for the GUI
Steps for making Bayesian classifier using GUI • The .m file is where we attach the appropriate code to the callback of each component • we are primarily concerned only with the callback functions • You don’t have to worry about any of the other function types
What Is a Callback? • It is a function that you write and associate with a specific component in the GUI or with the GUI figure itself • It controls GUI or component behavior by performing some action in response to an event for its component • The event can be • a mouse click on a push button • menu selection • key press , etc. • There is a set of callbacks for each component and for the GUI figure itself. • For more information about callbacks you can visit this interested website http://www.mathworks.com/help/techdoc/creating_guis/f16-999606.html
Steps for making Bayesian classifier using GUI • Open up the .m file • In the MATLAB editor, click on the icon ,which will bring up a list of the functions within the .m • Select drawBtn_Callback.
Steps for making Bayesian classifier using GUI • Using handles • Handles is a structure that contains all components in a figure including figure itself. • Texts, pushbutton, axes , … etc. • We can’t access any component without using handles
Steps for making Bayesian classifier using GUI • There are many important functions : • Set • Set a value for the component property • Get • Get the value of the component property • Str2num • Convert from string to number • Num2str • Convert from number to string
Examples • To get the value of pw1_text string: • p1=get(handles.pw1_text,’string’); • To set 20 to Mu1 string: • set(handles.Mu1_text,’string’,20); • Exercise: • Set a value of Mu1 string to Mu2 string
guidata function • One of important function in gui • It updatesthe value of the component and keep that variable in our memory-workspace • That’s important to be used in other callback functions. • We can achieve it by doing this: guidata(h,handles) • Generally speaking, we need to finish every callback function with it.
Dealing with inputs • In each text callback we should write these instructions: handles.m1=get(handles.m1,’string’); guidata(h,handles); • Where m1 is a variabe we define to store mean value that the user enterd in m1 text box • In any callback we can use it as handles.m1
Classify button callback Function classifyBtn_Callback(hObject, eventdata, handles) • m1=str2num(get(handles.m1,'string')); • m2=str2num(get(handles.m2,'string')); • s1=str2num(get(handles.s1,'string')); • s2=str2num(get(handles.s2,'string')); • p1=str2num(get(handles.p1,'string')); • p2=str2num(get(handles.p2,'string')); • x=str2num(get(handles.pattern,'string')); • Pxw1=NormalFun(x,m1, s1); • Pxw2=NormalFun(x,m2, s2); • Pw1x=Pxw1*p1/((Pxw1*p1)+(Pxw2*p2)); • Pw2x=Pxw2*p2/((Pxw1*p1)+(Pxw2*p2)); • %Now make a decision using the posteriors; • if (Pw1x > Pw2x) • rslt = sprintf('%d belongs to w1', x); • set(handles.result,'string',rslt) • else • rslt = sprintf('%d belongs to w2', x); • set(handles.result,'string',rslt) • end
Function DrawBtn_Callback(hObject, eventdata, handles) Draw button callback • m1=str2num(get(handles.m1,'string')); • m2=str2num(get(handles.m2,'string')); • s1=str2num(get(handles.s1,'string')); • s2=str2num(get(handles.s2,'string')); • p1=str2num(get(handles.p1,'string')); • p2=str2num(get(handles.p2,'string')); • x=str2num(get(handles.pattern,'string')); • x1Max=m1+(4*s1); x1Min=m1-(4*s1); • x2Max=m2+(4*s2); x2Min=m2-(4*s2); • xMax=max(x1Max,x2Max); xMin=min(x1Min,x2Min); • x=xMin:0.1:xMax; • n=length(x); pxw1=zeros(1,n); pxw2=zeros(1,n); pw1x=zeros(1,n); pw2x=zeros(1,n); • for i=1:n • pxw1(i)=NormalFun(x(i),m1, s1); pxw2(i)=NormalFun(x(i),m2, s2); • end • for i=1:n • pw1x(i)=pxw1(i)*p1/((pxw1(i)*p1)+(pxw2(i)*p2)); • pw2x(i)=pxw2(i)*p2/((pxw1(i)*p1)+(pxw2(i)*p2)); • end • axes(handles.axes1); • plot(x,pw1x,'r-',x,pw2x,'b'); • axes(handles.axes2); • plot(x,pxw1,'r-',x,pxw2,'b');
Launching the GUI • There are two ways to launch your GUI • through the GUIDE editor press the icon on the GUIDE editor • from the MATLAB command prompt • type in the name of the GUI at the command prompt • you don’t need to type the .fig or .m extension
References • Materials of this lab are prepared using: • http://blinkdagger.com/matlab/matlab-gui-graphical-user-interface-tutorial-for-beginners/ • http://www.matrixlab-examples.com/callback-function.html • http://www.mathworks.com/help/techdoc/creating_guis/f16-999606.html