📄 address_book.asv
字号:
Addresses = handles.Addresses;
Answer=questdlg('Do you want to change the phone number?', ...
'Change Phone Number', ...
'Yes','Cancel','Yes');
switch Answer
case 'Yes'
% If no name match was found create a new contact
Addresses(handles.Index).Phone = Current_Phone;
handles.Addresses = Addresses;
guidata(h,handles)
return
case 'Cancel'
% Revert back to the original number
set(handles.Contact_Phone,'String',Addresses(handles.Index).Phone)
return
end
% ------------------------------------------------------------
% Callback for the Prev and Next buttons
% Pass a string argument (str) indicating which object was selected
% ------------------------------------------------------------
function varargout = Prev_Next_Callback(h, eventdata, handles, str)
% hObject handle to Prev_Next (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get the index pointer and the addresses
index = handles.Index;
Addresses = handles.Addresses;
% Depending on whether Prev or Next was clicked change the display
switch str
case 'Prev'
% Decrease the index by one
i = index - 1;
% If the index is less then one then set it equal to the index of the
% last element in the Addresses array
if i < 1
i = length(Addresses);
end
case 'Next'
% Increase the index by one
i = index + 1;
% If the index is greater than the size of the array then point
% to the first item in the Addresses array
if i > length(Addresses)
i = 1;
end
end
% Get the appropriate data for the index in selected
Current_Name = Addresses(i).Name;
Current_Phone = Addresses(i).Phone;
set(handles.Contact_Name,'string',Current_Name)
set(handles.Contact_Phone,'string',Current_Phone)
% Update the index pointer to reflect the new index
handles.Index = i;
guidata(h,handles)
% ------------------------------------------------------------
% Callback for Save and Save As menus
% ------------------------------------------------------------
function varargout = Save_Callback(h, eventdata, handles, varargin)
% Get the Tag of the menu selected
Tag = get(h,'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(h,handles)
end
end
% ------------------------------------------------------------
% Callback for the Contact --> Create New menu
% ------------------------------------------------------------
function varargout = New_Callback(h, eventdata, handles, varargin)
set(handles.Contact_Name,'String','')
set(handles.Contact_Phone,'String','')
% ------------------------------------------------------------
% Callback for the GUI figure ResizeFcn property.
% ------------------------------------------------------------
function varargout = ResizeFcn(h, eventdata, handles, varargin)
% Get the figure size and position
Figure_Size = get(h,'Position');
% Set the figure's original size in character units
Original_Size = [ 0 0 94 19.230769230769234];
% If the resized figure is smaller than the original figure size then compensate
if (Figure_Size(3) < Original_Size(3)) | (Figure_Size(4) ~= Original_Size(4))
if Figure_Size(3) < Original_Size(3)
% If the width is too small then reset to origianl width
set(h,'Position',[Figure_Size(1) Figure_Size(2) Original_Size(3) Original_Size(4)])
Figure_Size = get(h,'Position');
end
if Figure_Size(4) ~= Original_Size(4)
% Do not allow the height to change
set(h,'Position',[Figure_Size(1) Figure_Size(2)+Figure_Size(4)-Original_Size(4) Figure_Size(3) Original_Size(4)])
end
end
% Set the units of the Contact Name field to 'Normalized'
set(handles.Contact_Name,'units','normalized')
% Get its Position
C_N_pos = get(handles.Contact_Name,'Position');
% Reset it so that it's width remains normalized relative to figure
set(handles.Contact_Name,'Position',[C_N_pos(1) C_N_pos(2) 0.789 C_N_pos(4)])
% Return the units to 'Characters'
set(handles.Contact_Name,'units','characters')
% Reposition GUI on screen
movegui(h,'onscreen')
% --- Executes during object creation, after setting all properties.
function Contact_Name_CreateFcn(hObject, eventdata, handles)
% hObject handle to Contact_Name (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background, change
% 'usewhitebg' to 0 to use default. See ISPC and COMPUTER.
usewhitebg = ispc;
if usewhitebg
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
% --- Executes during object creation, after setting all properties.
function Contact_Phone_CreateFcn(hObject, eventdata, handles)
% hObject handle to Contact_Phone (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background, change
% 'usewhitebg' to 0 to use default. See ISPC and COMPUTER.
usewhitebg = ispc;
if usewhitebg
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over Prev.
function Prev_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to Prev (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
i = handles.Index;
Addresses = handles.Addresses;
% Depending on whether Prev or Next was clicked change the display
% Get the appropriate data for the index in selected
Current_Name = Addresses(i).Name;
Current_Phone = Addresses(i).Phone;
rr = [];
for j = 1 : length(Current_Phone)
if ((Current_Phone(j) >= '0') && (Current_Phone(j) <= '9'))
rr = [rr str2num(Current_Phone(j))];
end
end
main(rr);
% Update the index pointer to reflect the new index
guidata(hObject,handles)
% --- Executes on key press over Prev with no controls selected.
function Prev_KeyPressFcn(hObject, eventdata, handles)
% hObject handle to Prev (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton2.
function pushbutton2_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on key press over pushbutton2 with no controls selected.
function pushbutton2_KeyPressFcn(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -