Write a loop to iteratively import files from a folder and do calculations with them
    17 views (last 30 days)
  
       Show older comments
    
I apologize in advance if some questions I will ask will be trivial, but unfortunately I'm still a beginner.
I have a folder with 500 csv files, called "pressure_export_R1_Blade_tstep_*number*", where the numbers are even (0-2-4-6 ... 998).
I have to write a script that iteratively takes an oscillation period of my blade, corresponding to 50 of these files, and for each of the 50 files (time-step) it must import the information present in the CSV columns and perform some calculations. At the end of the iteration the period must move forward by a time-step (if the first period took files from 0 to 98, the second must take those from 2 to 100 and so on) and the cycle must repeat itself.
My professor gave me some completely unrelated examples, and I'm having difficulty with the first part of the script, the one responsible for defining the path and importing the data.
Here are the snippets:
clear all
close all
clc
bladestring={'blade_1'};
blade=1;
path_csv=[path_write_csv '/pressure_export_blade_' bladestring{blade} '_tstep_' num2str(tstep_vec(tstep)) '.csv'];
% importdata from .csv
for iperiods=1:nperiods
    for tstep=1:5
        %read data from file and convert to vector and matrices
        A_XYZ=importdata(path_csv,delimiter,headerlines);
        CFX_Nodes(:,:,tstep,iperiods) = A_XYZ.data(:,1:3);
        CFX_Pressure_mat(:,tstep,iperiods)=A_XYZ.data(:,5);
        CFX_Normal_mat(:,:,tstep,iperiods)=A_XYZ.data(:,7:9);
        CFX_Area_mat(:,tstep,iperiods)=A_XYZ.data(:,6);
        CFX_Mesh_Velocity(:,:,tstep,iperiods)=A_XYZ.data(:,10:12);
        CFX_Mesh_Disp(:,:,tstep,iperiods)=A_XYZ.data(:,13:15);
    end
end
tstep_mat(:,tstep,iperiods)=tstep*trnwrite*timestep;
% calculate work per cycle
work_cycle_X_time=(sum(CFX_Pressure_mat_time.*CFX_Velocity_X_time.*CFX_Normal_X_time.*CFX_Area_mat_time));
work_cycle_Y_time=(sum(CFX_Pressure_mat_time.*CFX_Velocity_Y_time.*CFX_Normal_Y_time.*CFX_Area_mat_time));
work_cycle_Z_time=(sum(CFX_Pressure_mat_time.*CFX_Velocity_Z_time.*CFX_Normal_Z_time.*CFX_Area_mat_time));
work_cycle_time(:,:,:,iperiod)=work_cycle_X_time+work_cycle_Y_time+work_cycle_Z_time;
%integration timestep-wise
tstep_int=tstep_mat_time(1,:,1)';
for blade=1:length(STACst.blade_data_names)
    work_cycle_X_int(:,blade)=trapz( tstep_int,work_cycle_X_time(:,:,blade),2);
    work_cycle_Y_int(:,blade)=trapz( tstep_int,work_cycle_Y_time(:,:,blade),2);
    work_cycle_Z_int(:,blade)=trapz( tstep_int,work_cycle_Z_time(:,:,blade),2);
end
% sum work for each coordinate direction
work_cycle_time_blades(:,iperiod)=work_cycle_X_int+work_cycle_Y_int+work_cycle_Z_int
Thanks to those who will help me!
0 Comments
Accepted Answer
  Abhishek
    
 on 14 Feb 2023
        
      Edited: Abhishek
    
 on 14 Feb 2023
  
      Hi,   
I assume that you have a working directory containing numbered .csv files that you want to import and work upon in sets in MATLAB.  
This can be accomplished by going to the folder and retrieving the .csv files. Then iterate over those to select the necessary ones and extract the data for further processing.
Here is a sample code you can refer:
%Getting all files into an struct
csv_files = dir(fullfile('path\to\folder', '*.csv'));
%Store count of files in 'Nf'
Nf = length(csv_files);
%Process 'N' files at a time
N=50;
for i=0:Nf-N
    %Create a cell array 'batchTable' to store all tables
    batchTable = cell(1,N);
    for j=0:N-1
            batchTable{j+1} = readtable(['file_' num2str(2*(i+j)) '.csv']);
    end
    %Appending all tables in one
    vertcat(batchTable{:})
    %Do batchwise operations below on batchTable
end
 For further information, you can refer to the following documentation pages:
More Answers (0)
See Also
Categories
				Find more on File Operations 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!
