MATLAB Creating Graphical User Interfaces

(Barry) #1

8 Programming a GUIDE UI


% hObject handle to copy_menu_item (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
display('Copy menu item selected');

% --------------------------------------------------------------------
function tofile_menu_item_Callback(hObject, eventdata, handles)
% hObject handle to tofile_menu_item (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename,path] = uiputfile('myfile.m','Save file name');
The function names might be different in your UI, depending on the tag names you
specify in the GUIDE Menu Editor.

The callback functions trigger in response to these actions:


  • When the end user selects the Edit menu, the edit_menu_Callback function
    displays the text, 'Edit menu selected', in the MATLAB Command Window.

  • When the end user hovers the mouse over the Copy menu item, the
    copy_menu_item_Callback function displays the text, 'Copy menu item
    selected', in the MATLAB Command Window.

  • When the end user clicks and releases the mouse button on the To File menu item,
    the tofile_menu_item_Callback function displays a dialog box that prompts the
    end user to select a destination folder and file name.


The tofile_menu_item_Callback function calls the uiputfile function to prompt
the end user to supply a destination file and folder. If you want to create a menu item
that prompts the user for an existing file, for example, if your UI has an Open File
menu item, then use the uigetfile function.

When you create a cascading menu like this one, the intermediate menu items trigger
when the mouse hovers over them. The final, terminating, menu item triggers when the
mouse button releases over the menu item.

How to Update a Menu Item Check

You can add a check mark next to a menu item to indicate that an option is enabled. In
GUIDE, you can select Check mark this item in the Menu Editor to make the menu
item checked by default. Each time the end user selects the menu item, the callback
function can turn the check on or off.

This code shows how to change the check mark next to a menu item.
Free download pdf