10 views (last 30 days)

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

KSSV
on 26 Jun 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.

KSSV
on 26 Jun 2020

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

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.