MATLAB Creating Graphical User Interfaces

(Barry) #1
UI That Uses Persistent Data

Open and Load MAT-File


The address book UI contains a File > Open menu option for loading address book MAT-
files.


When you select this option, “Open_Callback” on page 9-11 in address_book.m
opens a dialog box that enables you to browse for files.


The dialog box returns the file name and the path to the file, which are passed
to fullfile to ensure the path is properly constructed for any platform. The
Check_And_Load function validates and loads the new address book.


For information on creating the menu, see “Create Menus for GUIDE UIs” on page 6-91.


Open_Callback


function Open_Callback(hObject, eventdata, handles, varargin)
[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 full file name and _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


Retrieve and Store Data


The Contact Name text box displays the name of the address book entry. If you type
in a new name and press enter, the “Contact_Name_Callback” on page 9-12 in
address_book.m does the following:



  • If the name exists in the current address book, the corresponding phone number
    displays.

  • If the name does not exist, a question dialog box asks you if you want to create a new
    entry, or cancel and return to the name previously displayed.

Free download pdf