MATLAB Creating Graphical User Interfaces

(Barry) #1
UI That Uses Persistent Data

strcmpi(varargin{1},'book') && ...
(2 == exist(varargin{2},'file')))
Check_And_Load(varargin{2},handles);
else
errordlg('File Not Found','File Load Error')
set(handles.Contact_Name,'String','')
set(handles.Contact_Phone,'String','')
end


MAT-file Validation


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” on page 9-9 function
in address_book.m validates and loads the data as follows:



  • Loads the specified file or the default if no file is specified.

  • Determines if the MAT-file is a valid address book.

  • Displays the data if it is valid. If the data is not valid, displays an error dialog box
    (errordlg).

  • Returns 1 for valid MAT-files and 0 if invalid (used by the Open menu callback).

  • Saves the following items in the handles structure:

    • The name of the MAT-file

    • The Addresses structure

    • An index pointer indicating which name and phone number are currently
      displayed in the UI




Check_And_Load


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)

Free download pdf