Not looping over all the files
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
KSSV
2022-6-10
编辑:KSSV
2022-6-10
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!