Want to see the results from the 79 csv files but only getting one result
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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.
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!