Sum cumulative value from all files that is summoned in a loop
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
SRRellum
el 20 de Abr. de 2022
Comentada: SRRellum
el 20 de Abr. de 2022
I want to add the row length of the variable 'time' in a file (length(dataset.time)) from multiple datasets that are summoned in a loop. My setup is a follows:
datafolder = ' Z:\Projects\data';
file = dir(datafolder);
for i = 3:105
patient = load(strcat(datafolder,'\',file(i).name));
dataset = patient.table
row_length = length(dataset.time)
sum_length = ?
For now, ' row_length' only outputs the number of rows from the last dataset.
How do I add the total number of rows from dataset 3:105?
Hope you can help
2 comentarios
Stephen23
el 20 de Abr. de 2022
Editada: Stephen23
el 20 de Abr. de 2022
This line:
for i = 3:105
As Walter Roberson wrote in that last link: "In short: if your code assumes that '.' and '..' are the first two entries in a directory, your code has a bug (even in MS Windows). If your code assumes that directory entries are returned in any sorted order, your code has a bug (in all OS.)"
Much better code uses ISMEMBER or SETDIFF.
Respuesta aceptada
Davide Masiello
el 20 de Abr. de 2022
Inside the loop write
row_length(i) = length(dataset.time);
After the loop, you can calculate the summ of all lengths with
sum_length = sum(row_length);
Más respuestas (1)
David Hill
el 20 de Abr. de 2022
datafolder = ' Z:\Projects\data';
file = dir(datafolder);
row_length = 0;
for i = 3:105
patient = load(strcat(datafolder,'\',file(i).name));
dataset = patient.table
row_length = row_length+length(dataset.time)
end
Ver también
Categorías
Más información sobre Downloads 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!