I have sensors values in the form of multiple csv's want to read each file and if the length is less than 800 take average of each column and insert in the last of csv

2 views (last 30 days)
shery khan
shery khan on 25 Jun 2022
Commented: shery khan on 26 Jun 2022
following is the code i am testing on single file where as i have multiple files that have missing values i want to know how can i check the length of file and ignore first column and if length is less then 800 row get the length first and take average of each column and add average at the end of file . suppose a file have 631 rows . so i need to fill left rows by average means 169 values are filled .
1st Row 2nd 3rd 4th
--
631Row
average value
average value
--
--
--
800 Rows
i have files that contain 4 columns but i am working on 3 columns first column is ignored
Kindly help me in this regard ..
tmp=load('D:\Testing\1464882962.m_proc_gyro.csv');
if(length(tmp)~=800)
for i=1:length(tmp)
Imp_three=tmp(:,2:end);
First_col=Imp_three(:,1);
Second_col=Imp_three(:,2);
Third_col=Imp_three(:,3);
M1=mean(First_col);
M2=mean(Second_col);
M3=mean(Third_col);
s1 = size(First_col,1); % Rows
s2 = size(Second_col,2);
s3 = size(Third_col,3);
MArray=[M1,M2,M3];
end
end

Accepted Answer

Voss
Voss on 25 Jun 2022
Edited: Voss on 25 Jun 2022
This will do that, including the first column (which you can continue to ignore):
tmp=load('D:\Testing\1464882962.m_proc_gyro.csv');
n_rows_target = 800; % extend to 800 rows, with mean of each column
n_rows = size(tmp,1);
if n_rows < n_rows_target
mean_tmp = mean(tmp,1);
tmp(n_rows+1:n_rows_target,:) = repmat(mean_tmp,n_rows_target-n_rows,1);
end
  6 Comments

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 25 Jun 2022

Community Treasure Hunt

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

Start Hunting!