Creating Graphical User Interfaces |
The Contact Name Callback
The Contact Name text box displays the name of the address book entry. If you type in a new name and press enter, the callback performs these steps:
questdlg
) asks you if you want to create a new entry or cancel and return to the name previously displayed.
Storing and Retrieving Data
This callback makes use of the handles
structure to access the contents of the address book and to maintain an index pointer (handles.Index
) that enables the callback to determine what name was displayed before it was changed by the user. The index pointer indicates what name is currently displayed. The address book and index pointer fields are added by the Check_And_Load function when the GUI is run.
If the user adds a new entry, the callback adds the new name to the address book and updates the index pointer to reflect the new value displayed. The updated address book and index pointer are again saved (guidata
) in the handles
structure.
Contact Name Callback
function Contact_Name_Callback(hObject, eventdata, handles)% Get the strings in the Contact Name and Phone text box
Current_Name = get(handles.Contact_Name,'string'); Current_Phone = get(handles.Contact_Phone,'string');% If empty then return
if isempty(Current_Name) return end% Get the current list of addresses from the handles structure
Addresses = handles.Addresses;% Go through the list of contacts
% Determine if the current name matches an existing name
for i = 1:length(Addresses) if strcmp(Addresses(i).Name,Current_Name) set(handles.Contact_Name,'string',Addresses(i).Name) set(handles.Contact_Phone,'string',Addresses(i).Phone) handles.Index = i; guidata(hObject, handles) return end end% If it's a new name, ask to create a new entry
Answer=questdlg('Do you want to create a new entry?', ... 'Create New Entry', ... 'Yes','Cancel','Yes'); switch Answer case 'Yes' Addresses(end+1).Name = Current_Name;% Grow array by 1
Addresses(end).Phone = Current_Phone; index = length(Addresses); handles.Addresses = Addresses; handles.Index = index; guidata(hObject, handles) return case 'Cancel'% Revert back to the original number
set(handles.Contact_Name,'String',Addresses(handles.Index).Name ) set(handles.Contact_Phone,'String',Addresses(handles.Index).Pho ne) return end
Loading an Address Book Into the Reader | The Contact Phone Number Callback |