Effacer les filtres
Effacer les filtres

list of telephone numbers

7 vues (au cours des 30 derniers jours)
Britnie Casillas
Britnie Casillas le 8 Nov 2019
Commenté : Jeremy le 8 Nov 2019
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 commentaires
Britnie Casillas
Britnie Casillas le 8 Nov 2019
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 le 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);

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by