Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
Reading a Text File
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi,
I'm a bit lost with Input/Output and text files on MATLAB. If I'm given a text file containing a list of names and telephone numbers e.g.
John Smith 415 278 9082
Gabriel Zoro 214 678 0929
How can I create a function to search for a someone's first and last name and then display the line with the phone number?
Thank you!!
댓글 수: 2
Walter Roberson
2016년 11월 19일
What do you know about the format of the file? Will each line always have exactly two names, separated by spaces? No "Dr", no middle names, no "Sr", no "von"?
Will each line always have exactly three portions of the phone number, separated by spaces? Does the phone number ever include '+' or '-' or '(' and ')' ?
답변 (1개)
Walter Roberson
2016년 11월 19일
searchfor = input('Name to look for?', 's');
fid = fopen('YourFile.txt', 'r');
matches = 0;
while true
thisline = fgetl(fid);
if ~ischar(thisline)
break; %end of file
end
if isempty( strtrim(thisline) )
continue; %just whitespace on this line
end
if strncmp( lower(thisline), lower(searchfor), length(searchfor))
matches = matches + 1;
fprintf('Match: %s\n', thisline);
end
end
if matches == 0
fprintf('No matches for "%s"\n', searchfor)
end
댓글 수: 0
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!