Want to see the results from the 79 csv files but only getting one result

1 view (last 30 days)
Hi,
I would like to read 79 the csv files in the folder then want to read the column_13 from all the csv files hence take the mean of column_13 from each csv files hence will give me 79 mean values hence further use the relation below
TurbulentFluctuation_Square = (TurbulentFluctuation_array).^2; % taking the square of the array of each array from each folder
TurbulentFluctuationSquare_Mean = mean(TurbulentFluctuation_Square); %taking the mean of the sqaure value
TurbulentStrength_Urms = sqrt(TurbulentFluctuationSquare_Mean) % sqrt of the mean value
Turbulent_Intensity = TurbulentStrength_Urms*(sqrt(TurbulentFluctuationArray_Mean)); % calculating turbulent intensity
To calculate them values from each mean values and save it into the new csv file but I am struggling to make a code. I made a code to read one file and it is working fine. But the code to work for all the csv file also giving me one result. Code is below if you can help please!
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
ranges = zeros(numel(N), 1);
% 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 = table2array(col_13) %convert the table to arrays
TurbulentFluctuationArray_Mean = mean(TurbulentFluctuation_array); %calculate the mean of each array from each folder
TurbulentFluctuation_Square = (TurbulentFluctuation_array).^2; % taking the square of the array of each array from each folder
TurbulentFluctuationSquare_Mean = mean(TurbulentFluctuation_Square); %taking the mean of the sqaure value
TurbulentStrength_Urms = sqrt(TurbulentFluctuationSquare_Mean) % sqrt of the mean value
Turbulent_Intensity = TurbulentStrength_Urms*(sqrt(TurbulentFluctuationArray_Mean)); % calculating turbulent intensity
end
csvwrite(fullfile(Q, 'output.csv'), TurbulentFluctuationSquare_Mean, TurbulentStrength_Urms,Turbulent_Intensity); % its only saving first term value TurbulentFluctuationSquare_Mean
  2 Comments
Andriy Voshchenko
Andriy Voshchenko on 10 Jun 2022
You are overwriting the result at each iteration. Try predefining the results matrix (if you know the final dimensions, if not you can skip this and matlab will automatically change the dimensions in each iteration, it will be less optimized but still works)
TurbulentStrength_Urms=zeros(N,1); % matrix of results that you will fill later in your "for cycle"
Inside your "for" loop change the results to the following:
TurbulentFluctuationArray_Mean(idx) = mean(TurbulentFluctuation_array);
TurbulentFluctuation_Square(idx) = (TurbulentFluctuation_array).^2;
TurbulentFluctuationSquare_Mean(idx) = mean(TurbulentFluctuation_Square(idx));
TurbulentStrength_Urms (idx)= sqrt(TurbulentFluctuationSquare_Mean(idx))
Turbulent_Intensity(idx) = TurbulentStrength_Urms(idx)*(sqrt(TurbulentFluctuationArray_Mean(idx)));
Simply add (idx) to indicate that at the iteration 1 you are writing the result at the first element of the matrix, iteration 2 results is written at the position 2 etc. This way you will have a vector/matrix (depending on your initial dimensions) with data saved at each iteration of your "for2 cycle.
muhammad choudhry
muhammad choudhry on 10 Jun 2022
that's the error I am getting now.
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in Turbulent_Intensity_line (line 17)
TurbulentFluctuation_Square(idx) = (TurbulentFluctuation_array).^2;

Sign in to comment.

Accepted Answer

KSSV
KSSV on 10 Jun 2022
Edited: KSSV on 10 Jun 2022
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
n = numel(N) ;
TurbulentFluctuationArray_Mean = zeros(n,1) ;
TurbulentFluctuationSquare_Mean = zeros(n,1) ;
TurbulentStrength_Urms = zeros(n,1) ;
Turbulent_Intensity = zeros(n,1) ;
% loop over the file names
for idx = 1:n
data = readtable( fullfile(P, N{idx}) ); % read the csv files
TurbulentFluctuation_array = data.(13) ;
TurbulentFluctuationArray_Mean(idx) = mean(TurbulentFluctuation_array); %calculate the mean of each array from each folder
TurbulentFluctuation_Square = TurbulentFluctuation_array.^2; % taking the square of the array of each array from each folder
TurbulentFluctuationSquare_Mean(idx) = mean(TurbulentFluctuation_Square); %taking the mean of the sqaure value
TurbulentStrength_Urms(idx) = sqrt(TurbulentFluctuationSquare_Mean(idx)) ; % sqrt of the mean value
Turbulent_Intensity(idx) = TurbulentStrength_Urms(idx)*(sqrt(TurbulentFluctuationArray_Mean(idx))); % calculating turbulent intensity
end
T = table(TurbulentFluctuationSquare_Mean,TurbulentStrength_Urms,Turbulent_Intensity) ;
writetable(fullfile(Q, 'output.csv'), T); % its only saving first term value TurbulentFluctuationSquare_Mean
  6 Comments
muhammad choudhry
muhammad choudhry on 10 Jun 2022
Error using writetable (line 179)
Incorrect argument order. writetable expects a table as its first argument and a filename as its second argument.
Error in Turbulent_Intensity_line (line 27)
writetable(fullfile(Q, 'results.csv'), T); % its only saving first term value TurbulentFluctuationSquare_Mean

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!