6 Lay Out a UI Using GUIDE
callback in the code file using a combination of the Tag field and the UI file name.
The callback's name does not display in the Callback field of the Menu Editor, but
selecting the menu item does trigger it.
You can also type an unquoted string into the Callback field to serve as a callback. It
can be any valid MATLAB expression or command. For example, the string
set(gca, 'Color', 'y')
sets the current axes background color to yellow. However, the preferred approach to
performing this operation is to place the callback in the UI code file. This avoids the
use of gca, which is not always reliable when several figures or axes exist. Here is a
version of this callback coded as a function in the UI code file:
function axesyellow_Callback(hObject, eventdata, handles)
% hObject handle to axesyellow (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.axes1,'Color','y')
This code sets the background color of the axes with Tag axes1 no matter to what
object the context menu is attached to.
If you enter a callback string in the Menu Editor, it overrides the callback for the item
in the code file, if any has been saved. If you delete a string you have entered in the
Callback field, the callback for the item in the UI code file is executed when the user
selects that item in the UI.
See “Menu Item” on page 8-22 for more information about specifying this field and
for programming menu items. For another example of programming context menus in
GUIDE, see “Synchronized Data Presentations in a GUIDE UI” on page 9-30.
The View button displays the callback, if there is one, in an editor. If you have not yet
saved the UI, GUIDE prompts you to save it.
- Open the Property Inspector, where you can change all menu properties except
callbacks, by clicking the More Properties button. For detailed information about
these properties, see Uicontextmenu Properties.
Associate the Context Menu with an Object
1 In the Layout Editor, select the object for which you are defining the context menu.
2 Use the Property Inspector to set this object's UIContextMenu property to the name
of the desired context menu.