Creating Graphical User Interfaces |
Loading an Address Book Into the Reader
There are two ways in which an address book (i.e., a MAT-file) is loaded into the GUI:
addrbook.mat
).
Validating the MAT-file
To be a valid address book, the MAT-file must contain a structure called Addresses
that has two fields called Name
and Phone
. The Check_And_Load
subfunction validates and loads the data with the following steps:
load
) the specified file or the default if none is specified.
errordlg
).
1
for valid MAT-files and 0
if invalid (used by the Open menu callback)
handles
structure:
This is the Check_And_Load
function.
function pass = Check_And_Load(file,handles)% Initialize the variable "pass" to determine if this is a valid % file.
pass = 0;% If called without any file then set file to the default file % name.
% Otherwise if the file exists then load it.
if isempty(file) file = 'addrbook.mat'; handles.LastFile = file; guidata(handles.Address_Book,handles) end if exist(file) == 2 data = load(file); end% Validate the MAT-file
% The file is valid if the variable is called "Addresses" and it % has fields called "Name" and "Phone"
flds = fieldnames(data); if (length(flds) == 1) & (strcmp(flds{1},'Addresses')) fields = fieldnames(data.Addresses); if (length(fields) == 2) &(strcmp(fields{1},'Name')) & (strcmp(fields{2},'Phone')) pass = 1; end end% If the file is valid, display it
if pass% Add Addresses to the handles structure
handles.Addresses = data.Addresses; guidata(handles.Address_Book,handles)% Display the first entry
set(handles.Contact_Name,'String',data.Addresses(1).Name) set(handles.Contact_Phone,'String',data.Addresses(1).Phone)% Set the index pointer to 1 and save handles
handles.Index = 1; guidata(handles.Address_Book,handles) else errordlg('Not a valid Address Book','Address Book Error') end
The Open Menu Callback
The address book GUI contains a File menu that has an Open submenu for loading address book MAT-files. When selected, Open displays a dialog (uigetfile
) that enables the user to browser for files. The dialog displays only MAT-files, but users can change the filter to display all files.
The dialog returns both the filename and the path to the file, which is then passed to fullfile
to ensure the path is properly constructed for any platform. Check_And_Load
validates and load the new address book.
Open_Callback Code Listing
function Open_Callback(hObject, eventdata, handles) [filename, pathname] = uigetfile( ... {'*.mat', 'All MAT-Files (*.mat)'; ... '*.*','All Files (*.*)'}, ... 'Select Address Book');% If "Cancel" is selected then return
if isequal([filename,pathname],[0,0]) return% Otherwise construct the fullfilename and Check and load the file
else File = fullfile(pathname,filename);% if the MAT-file is not valid, do not save the name
if Check_And_Load(File,handles) handles.LastFIle = File; guidata(hObject, handles) end end
See the Creating Menus - The Menu Editor section for information on creating the menu.
Running the GUI | The Contact Name Callback |