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
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Voss
2022년 6월 25일
편집: Voss
2022년 6월 25일
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
Image Analyst
2022년 6월 25일
This answer works for one specific file. You said "i have multiple files" so to solve that part, make sure to check my answer below where I direct you to the FAQ.
Since I didn't see looping over multiple files in your code, I think maybe you couldn't figure out how to adapt it, so I'll do it for you below.
% Specify the folder where the files live.
myFolder = 'D:\Testing';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.csv'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
% Loop over all CSV files in the folder.
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now read it in as an array with readmatrix()
thisMatrix = readmatrix(fullFileName);
% Now process thisMatrix as you have been doing.
end
But please, still remember to check out the FAQ because there is lots of other good information in there.
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!