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

1 回表示 (過去 30 日間)
muhammad choudhry
muhammad choudhry 2022 年 6 月 10 日
コメント済み: KSSV 2022 年 6 月 10 日
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 件のコメント
Andriy Voshchenko
Andriy Voshchenko 2022 年 6 月 10 日
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 2022 年 6 月 10 日
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;

サインインしてコメントする。

採用された回答

KSSV
KSSV 2022 年 6 月 10 日
編集済み: KSSV 2022 年 6 月 10 日
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 件のコメント
muhammad choudhry
muhammad choudhry 2022 年 6 月 10 日
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
KSSV
KSSV 2022 年 6 月 10 日
writetable(T,fullfile(Q, 'output.csv'));

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by