Not looping over all the files

1 view (last 30 days)
Hi,
I am reading the column_13 from 79 csv files in a folder then taking the mean of each column from each file and want to save 79 values of means in a separate file but struggling to do that. Below is the code. Please see the picture attached shows the workspace and error.
Code:
close all; clear all; clc;
P = 'F:\3-PIV_Experimental_Data\Calculations_TurbulentIntensity\line_Data\Elliptical_Side\Length\DesignPoint\110_outlet';
Q = 'F:\3-PIV_Experimental_Data\Calculations_TurbulentIntensity\line_Data\Elliptical_Side\Length\DesignPoint';
S = dir(fullfile(P,'*.csv'));
N = natsortfiles({S.name});
% Pre-allocate output vector
TurbulentFluctuationArray_Mean=zeros(numel(N), 1); % matrix of results that you will fill later in your "for cycle"
% loop over the file names
for idx = 1:numel(N,1);
data = readtable( fullfile(P, N{idx}) ); % read the csv files
col_13 = data(:,13); % Get the 13th column
TurbulentFluctuation_array(idx) = table2array(col_13) %convert the table to arrays
TurbulentFluctuationArray_Mean(idx) = mean(TurbulentFluctuation_array);
end
csvwrite(fullfile(Q, 'TF_mean.csv'), TurbulentFluctuationArray_Mean(idx)); % its only saving first term value TurbulentFluctuationSquare_Mean

Accepted Answer

KSSV
KSSV on 10 Jun 2022
Edited: KSSV on 10 Jun 2022
close all; clear all; clc;
P = 'F:\3-PIV_Experimental_Data\Calculations_TurbulentIntensity\line_Data\Elliptical_Side\Length\DesignPoint\110_outlet';
Q = 'F:\3-PIV_Experimental_Data\Calculations_TurbulentIntensity\line_Data\Elliptical_Side\Length\DesignPoint';
S = dir(fullfile(P,'*.csv'));
N = natsortfiles({S.name});
% Pre-allocate output vector
TurbulentFluctuationArray_Mean=zeros(numel(N), 1); % matrix of results that you will fill later in your "for cycle"
% loop over the file names
for i = 1:numel(N);
data = readtable( fullfile(P, N{i}) ); % read the csv files
col_13 = data.(13) ; % Get the 13th column
TurbulentFluctuationArray_Mean(i) = mean(col_13);
end
csvwrite(fullfile(Q, 'TF_mean.csv'), TurbulentFluctuationArray_Mean); % its only saving first term value TurbulentFluctuationSquare_Mean
  3 Comments
KSSV
KSSV on 10 Jun 2022
Edited the code....

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!