indexing a loop to run through multiple files

I have experimental data in multiple csv files.
Ive written the code I want and checked this works by only inputting 1 specific file, but now I am trying to write in a loop to do the same for all the files. I am struggling with the indexing to get this to work, could anybody help?
%% input csv files
file = dir('*.csv'); %read the files into matlab
num_files = length(file); %record how many files have been found
[~, index] = natsort({file.name}); %sort the files into order
filelist = file(index);
%% Calculate granular temperature
for a = 1:length(num_files)
%read the table into matlab
table = table2array(readtable(filelist(a).name));
%save the particle ID's and velocity components
T = table(:, [1:4]);
%calculate resultant velocity for each particle (sqrt of the velcoities squared)
ResV = sqrt((T(:,2).^2) + (T(:,3).^2) + (T(:,4).^2));
%calculate mean resultant velocity of all the particles
ResVav = mean(ResV)
%calculate the average granular temperature
GTav = mean(ResV-ResVav)
end

 Respuesta aceptada

Yongjian Feng
Yongjian Feng el 7 de Jul. de 2021
Try this:
%% input csv files
file = dir('*.csv'); %read the files into matlab
num_files = length(file); %record how many files have been found
sorted = sort({file.name});
for a = 1:length(num_files)
%read the table into matlab
table = table2array(readtable(sorted{a}));
%save the particle ID's and velocity components
T = table(:, [1:4]);
%calculate resultant velocity for each particle (sqrt of the velcoities squared)
ResV = sqrt((T(:,2).^2) + (T(:,3).^2) + (T(:,4).^2));
%calculate mean resultant velocity of all the particles
ResVav = mean(ResV)
%calculate the average granular temperature
GTav = mean(ResV-ResVav)
end

5 comentarios

C.G.
C.G. el 7 de Jul. de 2021
Editada: C.G. el 7 de Jul. de 2021
Thankyou for your response.
I have tried the sort function you use but it doesnt sort my files into the correct order, it has done 1,10,11... etc.
Also I ran your code and it is still telling me that a is 1, when num_files is 40. This is what it was doing when I tried myself
I see. Just a small mistake there.
%% input csv files
file = dir('*.csv'); %read the files into matlab
num_files = length(file); %record how many files have been found
sorted = sort({file.name});
for a = 1:num_files
%read the table into matlab
table = table2array(readtable(sorted{a}));
%save the particle ID's and velocity components
T = table(:, [1:4]);
%calculate resultant velocity for each particle (sqrt of the velcoities squared)
ResV = sqrt((T(:,2).^2) + (T(:,3).^2) + (T(:,4).^2));
%calculate mean resultant velocity of all the particles
ResVav = mean(ResV)
%calculate the average granular temperature
GTav = mean(ResV-ResVav)
end
C.G.
C.G. el 7 de Jul. de 2021
Thank you that works for me now.
Just another question, but in each iteration of the loop I want the variable GTav to save so I can plot this up. Is there a way to save this value each time?
Yongjian Feng
Yongjian Feng el 7 de Jul. de 2021
Editada: Yongjian Feng el 7 de Jul. de 2021
Yes. Try this:
%% input csv files
file = dir('*.csv'); %read the files into matlab
num_files = length(file); %record how many files have been found
sorted = sort({file.name});
result = [];
for a = 1:num_files
%read the table into matlab
table = table2array(readtable(sorted{a}));
%save the particle ID's and velocity components
T = table(:, [1:4]);
%calculate resultant velocity for each particle (sqrt of the velcoities squared)
ResV = sqrt((T(:,2).^2) + (T(:,3).^2) + (T(:,4).^2));
%calculate mean resultant velocity of all the particles
ResVav = mean(ResV)
%calculate the average granular temperature
GTav = mean(ResV-ResVav);
result(end+1) = GTav;
end
result
C.G.
C.G. el 7 de Jul. de 2021
Thank you for your help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Etiquetas

Preguntada:

el 7 de Jul. de 2021

Comentada:

el 7 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by