list of telephone numbers
7 views (last 30 days)
Show older comments
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 Comments
Jeremy
on 8 Nov 2019
You could use
strtok
to break apart the names, if it is always <firstname lastname> then
[firstname,s] = strtok(tline);
[lastname,s2] = strtok(s);
Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!