Writing the Code for the GUI

  1. Click on the mfile icon icon on the GUI figure to bring up the accompanying .m file.

  2. The first thing we need to do is to map a function to a keyboard press event. This basically means that whenever any keyboard key is pressed, the designated function is called. Also, we need to define a variable to hold the histogram data. We can accomplish this with the following code, which should be placed in the opening function before the line guidata(hObject,handles):

    %create an array of 9 zeroes
    handles.cats = zeros(1,9);
     
    %whenever any key is pressed, myFunction is called
    set(handles.figure1,'KeyPressFcn',@myFunction)
     
    %if we wanted to make it so the function is called when the key is released,
    %you would use set(handles.figure1,'KeyReleaseFcn',@myFunction) instead
  3. Next, we need to define myFunction. Paste the following code into the end of the m-file.

    function myFunction(src,evnt)
    %this function takes in two inputs by default
     
    %src is the gui figure
    %evnt is the keypress information
     
    %this line brings the handles structures into the local workspace
    %now we can use handles.cats in this subfunction!
    handles = guidata(src);
     
    %get the value of the key that was pressed
    %evnt.Key is the lowercase symbol on the key that was pressed
    %so even if you tried "shift + 8", evnt.Key will return 8
    k=str2num( evnt.Key );
     
    if k >0 & k < 10
        axes(handles.axes1); %select the axes to plot on
     
        %update the array containing the histogram information
        handles.cats(k)=handles.cats(k)+1;
     
        %create the histogram graph
        bar(handles.cats);
     
        %updates the handles structures!
        guidata(src, handles); 
     
    end
     
    %if k is zero, terminate the gui and put the data onto a new figure
    if k==0
        %get the position and unit type of the GUI figure
        guiWindowPosition = get(src,'Position');
        guiWindowUnits = get(src,'Units');     
        close(src); %close the gui
        finalWindow=figure; %create a new figure
     
        %set the position and units of the new figure
        set(finalWindow,'Units',guiWindowUnits)
        set(finalWindow,'Position',guiWindowPosition)
     
        %populate the new figure with the histogram
        bar(handles.cats);
    end
  4. Now save and run your GUI, you should see the following appear:

    Run the GUI

  5. Try pressing some keys to verify that it works. When you are finished, press 0 to close the GUI. Once you do this, the GUI will close but a new figure will replace it. At this point any further keyboard input will have no effect on the figure.

  6. An Advanced Example using a Combination of Input Keys

    In the previous example, we kept it simple and only used numerical key presses. But most “hotkeys” or “keyboard shortcuts” involve the use of alt, control, or shift. Luckily, Matlab is able to handles these cases as well!

    The evnt input parameter from MyFunction is a structure that contains three fields of information. Using this information, we can construct more complex key input combinations.

    • Character - The character displayed as a result of the key(s) released.
    • Modifier - This field is a cell array that contains the names of one or
      more modifier keys that the user releases (i.e., control, alt, shift, or empty
      if no modifier keys were released). On Macintosh computers, MATLAB can also
      return command.
    • Key - The lower case label on key that was released.
    1. Lets try another example wherein the GUI will respond when you press “+” or when you press the combination of “control” + “alt + “r”.

    2. Cut and paste this code into the beginning of myFunction:

      %initialize these variables
      control = 0;
      alt = 0;
      shift = 0;
       
      %determine which modifiers have been pressed
      for x=1:length(evnt.Modifier)
          switch(evnt.Modifier{x})
              case 'control'
                  control = 1;
              case 'alt'
                  alt = 1;
              case 'shift'
                  shift = 1;
          end
      end
       
      %if the following combination is pressed, then the following text
      %is displayed at the command prompt
      if (control ==1 && alt==1 && strcmp(evnt.Key,'r'))
          disp('I just pressed the combination of control, alt, and r!');
      end
       
      %if this character is pressed on the keyboard, it will display the 
      %following text.  Noticed evnt.Character was used, and not evnt.Key!
      if(strcmp(evnt.Character,'+'));
          disp('I just pressed the + key!');
      end
    3. Now, run the GUI again and try to input in “+” and the combination of “control” + “alt” + “r”. You should see the appropriate messages at the Matlab Command Prompt!

    Passing Additional Input Arguments

    You can define the callback function to accept additional input arguments by adding them to the function definition. For example:

    function myFunction(src,evnt,arg1,arg2)

    When using additional arguments for the callback function, you must set the value of the property to a cell array (i.e., enclose the function handle and arguments in curly braces). For example:

    set(handles.figure1,'KeyPressFcn',{@myFunction,arg1,arg2})

    For more information you can visit this Matlab link and do an internal search (control + f) for “keyPressFcn”.

    This is the end of the tutorial.

    Pages: 1 2