How to modify "find" code to make it work for three data files?
1 回表示 (過去 30 日間)
古いコメントを表示
Hi,
I am trying to use the code below to find values that exceed certain value (0.018) in a set of files, and then count the number of rows and print them in a file. I would like to print all the counted rows number (I mean for all 3 files) in one single file. Would anyone please be able to help me correct the code? I have attached the files for your kind reference.
Thank you very much.
close all; clear all; clc;
Folder = cd;
N=3;
A = zeros(N, 3);
for k = 1:N;
A=sprintf('result_%d.txt',k);
B=A>=0.018;
C=A(B);
D=size(C,1);
E=D/1000;
fid=fopen(['Fragility_' num2str(44) '.txt'],'w');
fprintf(fid,'%f\n',E);
fclose(fid);
end
2 件のコメント
Jos (10584)
2018 年 2 月 16 日
You never read in any files!
The line A=sprintf('result_%d.txt',k); returns a string not a number. Therefore, all subsequent lines are rather meaningless.
I suggest you make a flowchart of your program, or write it in pseudo-code to see what steps you need to take.
採用された回答
Guillaume
2018 年 2 月 16 日
編集済み: Guillaume
2018 年 2 月 16 日
As Jos says in his comment, most of your code is rather meaningless. In fact, if one was to be picky, one could find a fault with almost every line (use of clear all, close all, cd, unused variables, meaningless variable names, convoluted way of counting number of elements above threshold, using size to get the number of element of vectors, pointless num2str, lack of 't' option for fprintf, etc.)
I assume this should do what you want:
numfiles = 3;
threshold = 0.018;
linesabovethreshold = zeros(numfiles, 1);
for fileidx = 1:numfiles
filecontent = dlmread(sprintf('result_%d.txt, fileidx));
assert(iscolumn(filecontent), 'file number %d has more than one column', fileidx);
linesabovethreshold(fileidx) = sum(filecontent >= threshold); %only works if file has only one column
end
dlmwrite('Fragility_44.txt', linesabovethreshold / 1000);
edit: forgot the 1000 division
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!