Creating Graphical User Interfaces |
Saving Changes to the Address Book from the Menu
When you make changes to an address book, you need to save the current MAT-file, or save it as a new MAT-file. The File submenus Save and Save As enable you to do this. These menus, created with the Menu Editor, use the same callback, Save_Callback
.
The callback uses the menu Tag
property to identify whether Save or Save As is the callback object (i.e., the object whose handle is passed in as the first argument to the callback function). You specify the menu's Tag
property with the Menu Editor.
Saving the Addresses Structure
The handles
structure contains the Addresses
structure, which you must save (handles.Addresses
) as well as the name of the currently loaded MAT-file (handles.LastFile
). When the user makes changes to the name or number, the Contact_Name_Callback
or the Contact_Phone_Callback
updates handles.Addresses
.
Saving the MAT-File
If the user selects Save, the save
command is called to save the current MAT-file with the new names and phone numbers.
If the user selects Save As, a dialog is displayed (uiputfile
) that enables the user to select the name of an existing MAT-file or specify a new file. The dialog returns the selected filename and path. The final steps include
fullfile
to create a platform-independent pathname.
save
to save the new data in the MAT-file.
handles
structure to contain the new MAT-file name.
guidata
to save the handles
structure.
Save_Callback Code Listing
function Save_Callback(hObject, eventdata, handles)% Get the Tag of the menu selected
Tag = get(hObject, 'Tag');% Get the address array
Addresses = handles.Addresses;% Based on the item selected, take the appropriate action
switch Tag case 'Save'% Save to the default addrbook file
File = handles.LastFile; save(File,'Addresses') case 'Save_As'% Allow the user to select the file name to save to
[filename, pathname] = uiputfile( ... {'*.mat';'*.*'}, ... 'Save as');% If 'Cancel' was selected then return
if isequal([filename,pathname],[0,0]) return else% Construct the full path and save
File = fullfile(pathname,filename); save(File,'Addresses') handles.LastFile = File; guidata(hObject, handles) end end
The Create New Menu
The Create New menu simply clears the Contact Name and Contact Phone # text fields to facilitate adding a new name and number. After making the new entries, the user must then save the address book with the Save or Save As menus. This callback sets the text String
properties to empty strings:
function New_Callback(hObject, eventdata, handles) set(handles.Contact_Name,'String','') set(handles.Contact_Phone,'String','')
Paging Through the Address Book -- Prev/Next | The Address Book Resize Function |
© 1994-2005 The MathWorks, Inc.