Books
in black and white
Main menu
Home About us Share a book
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

A Guide to MATLAB for Beginners and Experienced Users - Brian R.H.

Brian R.H., Roland L.L. A Guide to MATLAB for Beginners and Experienced Users - Cambrige, 2001. - 346 p.
Download (direct link): beginnersandex2001.pdf
Previous << 1 .. 40 41 42 43 44 45 < 46 > 47 48 49 50 51 52 .. 91 >> Next


The objects you create in the Layout Editor are inert within that window — you can't type text in the Edit Text box, you can't see the additional options by clicking on the Popup Menu, etc. But in an activated GUI window, objects such as Toggle Buttons and Popup Menus will respond to mouse clicks. However, they will not actually perform any functions until you write a callback function for each of them. 132

Chapter 8: SIMULINK and GUIs

GUI Callback Functions

When you are ready to create a callback function for a given object, click the right mouse button on the object and select Edit Callback. The M-file associated with the GUI will be brought to the front in an Editor/Debugger window, with the cursor in a block of lines like the ones below. (If you haven't yet saved the GUI, you will be prompted to do so first, so that GUIDE knows what name to give the M-file.)

function varargout = pushbuttonl—Callback(h, eventdata, handles, varargin) % Stub for Callback of the uicontrol handles.pushbuttonl. disp('pushbuttonl Callback not implemented yet.')

% ------------------- end pushbuttonl—Callback -----------------------

In this case we have assumed that the object you selected was the first Push Button that you created in the Layout Editor; the string "pushbuttonl" above is its default tag. (Another way to find the tag for a given object is to select it and look next to "Tag" in the Property Inspector.) All you need to do now to bring this Push Button to life is to replace the disp command line in the template shown above with the commands that you want performed when the user clicks on the button. Of course you also need to save the M-file, which you can do in the usual way from the Editor/Debugger, or by activating the GUI from the Layout Editor. Each time you save or activate a GUI, a block of four lines like the ones above is automatically added to the GUI's M-file for any new objects or menu items that you have added to the GUI and that should have callback functions.

In the example plotgui from the previous section, there is one case where we used an existing MATLAB command as a callback function. For the Push Button labeled "Change axis limits", we simply entered axlimdlg into its callback function in lo i. .Thiscommandopensa dialog box that allows a user to type new values for the ranges of the x and y axes. MATLAB has a number of dialog boxes that you can use either as callback functions or in an ordinary M-file. For example, you can use inputdlg in place of input. Type help uitools for information on the available dialog boxes.

For the Popup Menu on the right side of the GUI, we put the following lines into its callback function template:

switch get(h, 'Value') case l

axis equal case 2

axis normal Graphical User Interfaces (GUIs) 133

case 3

axis square end

Each time the user of the GUI selects an item from a Popup Menu, MATLAB sets the "Value" property of the object to the line number selected and runs the associated callback function. As we described in Chapter 5, you can use get to retrieve the current setting of a property of a graphics object. When you use the callback templates provided by GUIDE as we have described, the variable h will contain the handle (the required first argument of get and set) for the associated object. (If you are using another method to write callback functions, you can use the MATLAB command gcbo in place of h.) For our sample GUI, line 1 of the Popup Menu says "equal", and if the user selects line 1, the callback function above runs axis equal; line 2 says "normal"; etc.

? You may have noticed that in Figure 8-9 the Popup Menu says "normal" rather than "equal" as in Figure 8-8; that's because we set its "Value" property to 2 when we created the GUI, using the Property Inspector. In this way you can make the default selection something other than the first item in a Popup Menu or Listbox.

For the Push Button labeled "Plot it!", we wrote the following callback function:

set(handles.figure1, 'HandleVisibility', 'callback') eval(get(handles.edit1, 'String'))

Here handles.figure1 and handles.edit1 are the handles for the entire GUI window and for the Edit Text box, respectively. Again these variables are provided by the callback templates in GUIDE, and if you do not use this feature you can generate the appropriate handles with gcbf and findobj(gcbf, 'Tag', 'edit1') ,respectively. Thesecondlineofthecall-back function above uses get to find the text in the Edit Text box and then runs the corresponding command with eval. The first line uses set to make the GUI window accessible to graphics commands used within callback functions; if we did not do this, a plotting command run by the second line would open a separate figure window.

? Another way to enable plotting within a GUI window is to select Application Options from the Tools menu in the Layout Editor, and within the window that appears change "Command-line accessibility" to "On". This has the possible drawback of allowing plotting commands the 134
Previous << 1 .. 40 41 42 43 44 45 < 46 > 47 48 49 50 51 52 .. 91 >> Next