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!