How to modify "find" code to make it work for three data files?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Ismail Qeshta
el 15 de Feb. de 2018
Comentada: Ismail Qeshta
el 16 de Feb. de 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 comentarios
Jos (10584)
el 16 de Feb. de 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.
Respuesta aceptada
Guillaume
el 16 de Feb. de 2018
Editada: Guillaume
el 16 de Feb. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Whos en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!