Want to see the results from the 79 csv files but only getting one result
    4 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
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 Commenti
  Andriy Voshchenko
 il 10 Giu 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.
Risposta accettata
  KSSV
      
      
 il 10 Giu 2022
        
      Modificato: KSSV
      
      
 il 10 Giu 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 Commenti
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Data Type Conversion in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


