PATTERN RECOGNITION LAB 8 TA : Nouf Al-Harbi :: nouf200@hotmail.com Lab objective: 2 Dealing with GUI Make Bayesian Classifier using GUI 3 4 Steps for making Bayesian classifier using GUI Why use a GUI in MATLAB? 5 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 6 open up MATLAB File New GUI Final form we should have 7 8 Edit Text components 9 Static Text component 2 Pushbutton component 2 figures Steps for making Bayesian classifier using GUI 8 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 9 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 10 The Tag parameter is basically the variable name of this component The String parameter is the text of this component Steps for making Bayesian classifier using GUI 11 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.m contains file all the code for the GUI Steps for making Bayesian classifier using GUI 12 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? 13 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 14 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 15 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 16 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 17 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 18 One of important function in gui It updates the 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 19 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 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')); Classify button callback 20 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 21 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); 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)*p 1)+(pxw2(i)*p2)); pw2x(i)=pxw2(i)*p2/((pxw1(i)*p 1)+(pxw2(i)*p2)); end axes(handles.axes1); x=xMin:0.1:xMax; n=length(x); pxw1=zeros(1,n); pxw2=zeros(1,n); pw1x=zeros(1,n); pw2x=zeros(1,n); plot(x,pw1x,'r',x,pw2x,'b'); axes(handles.axes2); plot(x,pxw1,'r',x,pxw2,'b'); Launching the GUI 22 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 23 References 24 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