How to Run a Code for all files in a folder

Hi All!
Am a bit new to Matlab, I have written a code to process csv data and I have a folder full of separate csv's which I would like to run the code on. I'm currently reading each csv into a matrix by referencing the full file name, but I don't really want to change the matrix source for every csv file I have. The final output from my code is a matrix called Graph_Matrix, is it possible to save this matrix for each csv file I run on the code?
function[Graph_Matrix] = transformdata()
%reading in a matrix from the file which I've directly referenced
A = readmatrix('2020_06_25_14_09_19_MetaWear_acc.csv')
%code proceeds
t = A(:,1);
x = A(:,2);
y = A(:,3);
z = A(:,4);
%convert t to appropriate time steps
time_steps = zeros(size(t));
for i = 2:length(t)
time_steps(i) = t(i)-t(i-1)
i = i + 1;
end
%converting time steps to seconds assuming the frequency of the measurement
%is 400Hz
time_step_seconds = time_steps * 0.0025;
time_cumulative = zeros(size(t));
time_cumulative(1) = time_step_seconds (1);
for i = 2:length(t)
time_cumulative(i) = time_step_seconds(i) + time_cumulative(i-1)
i = i + 1;
end
%%
%Converting accelerometer data to accelerations in m/s2
xAcc = x * 9.8;
yAcc = y * 9.8;
zAcc = z * 9.8;
Acc = [xAcc, yAcc, zAcc];
%Calculating the Acceleration Magnitude
AccMag = sqrt(xAcc.^2 + yAcc.^2 + zAcc.^2);
%Converting accelerometer data to velocities in m/s
xVel = zeros(size(xAcc));
yVel= zeros(size(yAcc));
zVel = zeros(size(zAcc));
for i = 2:length(xAcc)
xVel(i) = xAcc(i)*time_step_seconds(i) + xVel(i-1);
yVel(i) = yAcc(i)*time_step_seconds(i) + yVel(i-1);
xVel(i) = zAcc(i)*time_step_seconds(i) + zVel(i-1);
i = i + 1;
end
Vel = [xVel, yVel, zVel];
VelMag = [xVel.^2 + yVel.^2 + zVel.^2];
Graph_Matrix = [time_cumulative, AccMag, VelMag];
end

 Réponse acceptée

KSSV
KSSV le 26 Juin 2020
You input the filename to the function transformdata and save the output into cells.
files = dir('*.csv') ;
N = length(files) ;
iwant = cell(N,1) ;
for i = 1:N
thisfile = files(i).name ;
Graph_Matrix = transformdata(thisfile) ;
iwant{i} = Graph_Matrix ;
end
If all the matrices are of same size, you can convert the cell into a 3D matrix using cat.

3 commentaires

Hilary Kada
Hilary Kada le 26 Juin 2020
Thanks! I copied this directly into my code cos i dont fully get it, and when i ran it I got an error for using transformdata(thisfile) because it said I had too many input arguments. When you say I should input the function into transformdata, in the command window do i go transformdata(filename) and then hit enter?
Change your atatched function to this:
function[Graph_Matrix] = transformdata(filename)
%reading in a matrix from the file which I've directly referenced
A = readmatrix(filename) ;
%code proceeds
t = A(:,1);
x = A(:,2);
y = A(:,3);
z = A(:,4);
%convert t to appropriate time steps
time_steps = zeros(size(t));
for i = 2:length(t)
time_steps(i) = t(i)-t(i-1)
i = i + 1;
end
%converting time steps to seconds assuming the frequency of the measurement
%is 400Hz
time_step_seconds = time_steps * 0.0025;
time_cumulative = zeros(size(t));
time_cumulative(1) = time_step_seconds (1);
for i = 2:length(t)
time_cumulative(i) = time_step_seconds(i) + time_cumulative(i-1)
i = i + 1;
end
%%
%Converting accelerometer data to accelerations in m/s2
xAcc = x * 9.8;
yAcc = y * 9.8;
zAcc = z * 9.8;
Acc = [xAcc, yAcc, zAcc];
%Calculating the Acceleration Magnitude
AccMag = sqrt(xAcc.^2 + yAcc.^2 + zAcc.^2);
%Converting accelerometer data to velocities in m/s
xVel = zeros(size(xAcc));
yVel= zeros(size(yAcc));
zVel = zeros(size(zAcc));
for i = 2:length(xAcc)
xVel(i) = xAcc(i)*time_step_seconds(i) + xVel(i-1);
yVel(i) = yAcc(i)*time_step_seconds(i) + yVel(i-1);
xVel(i) = zAcc(i)*time_step_seconds(i) + zVel(i-1);
i = i + 1;
end
Vel = [xVel, yVel, zVel];
VelMag = [xVel.^2 + yVel.^2 + zVel.^2];
Graph_Matrix = [time_cumulative, AccMag, VelMag];
end
Hilary Kada
Hilary Kada le 26 Juin 2020
awesome, thanks so much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Analysis dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by