how to read a text file and read it line by line?
이전 댓글 표시
I have a text file which contains these sample of data in each line:
11-11111
11-25519
32-68129
36-14111
and I need to compare a number like these in the program with each line. for example in the program there is the number 14-68129 and there is no much in the file for it. how can I do this. the name of file is 'num.txt'. thanks
댓글 수: 2
So white spaces separate numbers, and dashes are some sort of internal separators, which play a role comparable to the decimal point? And are you checking each line of the file for the presence of one of these composite numbers (like 14-68129), or do you have a set of numbers? Finally, what information do you need? Just a flag that indicates the presence in the file, a flag per line, a count per line, a count for the whole file?
Behrad kiani
2013년 10월 14일
채택된 답변
추가 답변 (2개)
If the string defining the composite number is stored in a variable named theNumber:
count = numel( strfind( fileread('num.txt'), theNumber )) ;
댓글 수: 1
I guess that I should have made an example .. file Behrad.txt contains
11-11111
11-25519
14-68129
32-68129
14-68129
36-14111
14-68129
then,
>> count = numel( strfind( fileread('Behrad.txt'), '14-68129' ))
count =
3
and a count of 0 means "not found" obviously.
Azzi Abdelmalek
2013년 10월 14일
편집: Azzi Abdelmalek
2013년 10월 14일
v={'11-11111';'11-25519';'32-68129';'36-14111'}
n= '14-68129'
idx=find(strcmp(v,n))
%To read v
fid=fopen('num.txt')
v=textscan(fid,'%s')
fclose(fid)
댓글 수: 3
Behrad kiani
2013년 10월 14일
Azzi Abdelmalek
2013년 10월 14일
What is the error message if there is any
Behrad kiani
2013년 10월 22일
카테고리
도움말 센터 및 File Exchange에서 Variables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!