CS 211 Lesson 31
Menus
Quote:
The pessimist sees difficulty in every opportunity. The optimist sees the opportunity in every difficulty. Winston Churchill
Lesson Objectives:
Be able to create standard menus for GUI programs.
Be able to create context menus linked to graphical objects.
Understand the benefits of using MATLAB pcode files.
Be able to create pcode files from m-files.
Lesson:
I. MATLAB Concepts
A. GUI menus
MATLAB GUIs may have two types of menus:
standard menus are placed on a GUI figure's menu bar.
context menus are associated with GUI objects and are raised by right clicking on the associated object.
Both types of menus may be created and added to a GUI using the guide's menu editor.
B. Standard menus
To start the menu editor while in guide, select the Tool menu and the Menu Editor... command.
To create a standard menu, select the Menu Bar tab in the lower left corner of the menu editor.
Definitions:
Menu - a "top level" menu. A Menu is displayed in the menu bar at the top of a figure window.
Menu item - a "lower level" menu. A single item of a list of items that appears when a Menu is selected by a user.
To create a Menu, select the "New Menu" icon in the upper left corner of the menu editor.
To create a Menu item, first select the "Menu" you want to add a Menu item to, and then select the "New Menu Item" command in the upper left corner of the menu editor.
Make sure you give each menu object an appropriate Tag property.
Menu components are represented by an uimenu object. Some important properties of an uimenu object include:
Property Description Options 'Enabled' Can the user select the menu item? 'on' or 'off' 'Visible' Can the user see the menu item? 'on' or 'off' 'Checked' Shows a check mark in front of the menu item? This can be used to indicate whether a menu option is 'on' or 'off' 'on' or 'off' 'Accelerator' Allows a user to select the menu item using a CTRL-? key keyboard input. letters A-Z C. Context menus
To create a context menu, select the Context Menus tab in the lower left corner of the menu editor.
A top-level context menu is represented by an uicontextmenu object.
The parent property of an uicontextmenu object is the figure in which the context menu appears (not necessarily the object with which the context menu is associated)
Menu items and sub-menus falling under a top-level context menu are represented by uimenu objects.
Context menus must be associated with a GUI object
The UIContextMenu property of the object must be set to the handle of the associated context menu.
The handle of a context menu is stored in the handles structure field with the same name as the object's Tag property value.
The following code associates the context menu with Tag property value CM_tag with a plotted line.
Line_handle = plot(X, Y);
set(Line_handle, 'UIContextMenu', handles.CM_tag)
D. MATLAB pcode
pcode (pseudo code) is a pre-parsed, intermediate code form of MATLAB code.
When MATLAB executes a function, it translates the function into pcode and the executes the pcode.
The pcode version of an m-file runs faster than the corresponding m-file because it is in a format closer to machine language (code the CPU understands)
Converting a program into pcode is important for GUIs -- which can run slowly due to high memory requirements.
Converting a program into pcode has the side benefit of preventing anyone from seeing the program's implementation.
The following command creates the pcode file my_program.p from the m-file my_program.m
pcode my_program
When both an m-file and a p-file with the same name are in the MATLAB path, the pcode file (and not to m-file) is run.
Changes to a m-file do not change the corresponding pcode file (you must rerun the pcode command).
In general, you should only use pcode for the final (deployed) version of your programs.
II. Good Programming Practices
Always give menu items good descriptive names. Good
descriptive names make a program easier to use, especially for new users.
Group menu items under standard menu headings. For example,
don't put menu items for cut/copy/paste under a "File" menu -- instead list
them in an "Edit" menu.
Use separators to group similar menu items in a menu list.
If a menu item opens a new dialog box, include ellipses (...) after the menu name. For example, if a "Load" menu item opens a file-browsers dialog box, the menu item should be labeled "Load...".
III. Algorithms
(none for this lesson)
Lab Work: Lab 31
References: Chapman Textbook: section 10.7-10.9