필터 지우기
필터 지우기

list of telephone numbers

조회 수: 7 (최근 30일)
Britnie Casillas
Britnie Casillas 2019년 11월 8일
댓글: Jeremy 2019년 11월 8일
I am trying to create a code that reads a file and search for a particular name and display the line with the first, last name, and the phone number
the file is:
Arthur Jones (365)271-8912
John Smith (011-44-235)135246
Simon Addison (699)987-6543
Rachel Jones (444)361-8990
Jean-Paul Maronne (011033-1)3482152
Hideo Takata (011-81-3)3456-1212
and my code is:
function phonebook(last, first)
fid = fopen('list_of_telephone_numbers.txt', 'r');
if fid == -1
disp('File not opened successfully')
else
while feof(fid)==0
tline = fgetl(fid);
a = strfind(tline, last);
b = strfind(tline, first);
if ~isempty(a) && ~isempty(b)
disp(tline)
end
end
end
end
In the command window, when I type
>>phonebook('Jones', 'Arther')
I get blank results
>> phonebook('Jones', 'Arther')
>>
  댓글 수: 3
Britnie Casillas
Britnie Casillas 2019년 11월 8일
Awkward... I didnt realize I was spelling the name wrong...
It worked when I spelled it right. So, how do you differentiate between the surname and forename?
Jeremy
Jeremy 2019년 11월 8일
You could use
strtok
to break apart the names, if it is always <firstname lastname> then
[firstname,s] = strtok(tline);
[lastname,s2] = strtok(s);

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by