フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How can I read a clomun from txt file?

1 回表示 (過去 30 日間)
Alberto Alvarez
Alberto Alvarez 2015 年 3 月 3 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hi everyone,
I need to read several txt files and one column of each one. I've attached one of those txt files so you can understand what I'm talking about easier. I would like to write a script that can open the txt file, read the OK/KO column and perform (number[KO]/number[OK+KO])*100 formula.
Thanks in advance, Alberto
  2 件のコメント
Stephen23
Stephen23 2015 年 3 月 3 日
Try attaching the files again.
Alberto Alvarez
Alberto Alvarez 2015 年 3 月 3 日
Here goes the txt file example.

回答 (2 件)

Shrirang
Shrirang 2015 年 3 月 3 日
Hi, You can use following code
Hope it helps you !!!
fileID = fopen('45_500_7.txt', 'r');
cntOK = 0;
cntKO = 0;
while ~feof(fileID)
tline = fgetl(fileID);
if ~isempty(strfind(tline,'OK'))
cntOK = cntOK + 1;
end
if ~isempty(strfind(tline,'KO'))
cntKO = cntKO + 1;
end
end
fclose(fileID);
result = (cntOK/(cntOK + cntKO))*100;

Stephen23
Stephen23 2015 年 3 月 4 日
編集済み: Stephen23 2015 年 5 月 22 日
Rather than using a slow loop you could use the much neater textscan, and simply use the field-specification string to select exactly which column you want to work with:
>> fid = fopen('45_500_7.txt','rt');
>> C = textscan(fid,'%*s%*d%*s%*s%s%*[^\n]','headerlines',2);
>> fclose(fid);
>> C = C{1};
>> 100 * sum(strcmp('KO',C)) / numel(C)
ans =
10

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by