How to Run a Code for all files in a folder
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!