Specifications

5 GUI Applications
5-42
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
Using
fullfile to create a platform-independent pathname.
Calling
save to save the new data in the MAT-file.
Updating the
handles structure to contain the new MAT-file name.
Calling
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)