How to modify "find" code to make it work for three data files?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Ismail Qeshta
am 15 Feb. 2018
Kommentiert: Ismail Qeshta
am 16 Feb. 2018
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 Kommentare
Jos (10584)
am 16 Feb. 2018
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.
Akzeptierte Antwort
Guillaume
am 16 Feb. 2018
Bearbeitet: Guillaume
am 16 Feb. 2018
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
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Whos finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!