How to modify "find" code to make it work for three data files?

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Jos (10584)
Jos (10584) il 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.
Ismail Qeshta
Ismail Qeshta il 16 Feb 2018
Modificato: Ismail Qeshta il 16 Feb 2018
Hi Jos. Many thanks for your comment and suggestion.
The steps are the following: I have three files named as "result_1.txt", "result_2.txt" and "result_3.txt".
1) I would like to find all values that exceed 0.018 in those files.
2) I would like to count how many values have been exceeded in every file by counting the number of rows in every output file.
3) These numbers of rows are divided by 1000.
4) The values found in step (3) are collected in one file, if possible.

Accedi per commentare.

Risposta accettata

Guillaume
Guillaume il 16 Feb 2018
Modificato: Guillaume il 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 Commenti
Ismail Qeshta
Ismail Qeshta il 16 Feb 2018
Thanks Guillaume for your comment. I am sorry that my code contained all the mistakes that you mentioned. I have actually summarized the steps for Jos, and they are mentioned below for your kind reference:
I have three files named as "result_1.txt", "result_2.txt" and "result_3.txt".
1) I would like to find all values that exceed 0.018 in those files.
2) I would like to count how many values have been exceeded in every file by counting the number of rows in every output file.
3) These numbers of rows are divided by 1000.
4) The values found in step (3) are collected in one file, if possible.
Ismail Qeshta
Ismail Qeshta il 16 Feb 2018
I cannot thank you enough Guillaume. Your code worked very well. Thank you very much again.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by