フィルターのクリア

how to read a text file and read it line by line?

3 ビュー (過去 30 日間)
Behrad kiani
Behrad kiani 2013 年 10 月 14 日
コメント済み: Cedric 2013 年 10 月 22 日
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 件のコメント
Cedric
Cedric 2013 年 10 月 14 日
編集済み: Cedric 2013 年 10 月 14 日
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
Behrad kiani 2013 年 10 月 14 日
a count for the whole file

サインインしてコメントする。

採用された回答

sixwwwwww
sixwwwwww 2013 年 10 月 14 日
Dear Behrad, here is the complete code for your problem:
fid = fopen(filename); % Insert here the file name with complete file path
cell_data = textscan(fid, '%s');
fclose(fid);
matrix_data = [cell_data{:}];
Codes = char(matrix_data{:});
A = abs(Codes);
[m, n] = size(A);
new_code = '36-14111'; % Insert here your value you want to search in file
B = abs(new_code);
Codestatus = 0;
for i = 1:m
if A(i, :) == B(1,:)
disp('Code found')
Codestatus = 1;
break;
end
end
if Codestatus == 0
disp('Code not found')
end
Good luck!
  1 件のコメント
Behrad kiani
Behrad kiani 2013 年 10 月 22 日
thanks.this is exactly what I wanted

サインインしてコメントする。

その他の回答 (2 件)

Cedric
Cedric 2013 年 10 月 14 日
編集済み: Cedric 2013 年 10 月 14 日
If the string defining the composite number is stored in a variable named theNumber:
count = numel( strfind( fileread('num.txt'), theNumber )) ;
  1 件のコメント
Cedric
Cedric 2013 年 10 月 22 日
編集済み: Cedric 2013 年 10 月 22 日
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
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 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 14 日
What is the error message if there is any
Behrad kiani
Behrad kiani 2013 年 10 月 22 日
thanks for your comment AZZI my internet access is very low and I couldn,t reply your comment. I found the answer below

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by