Write a loop to iteratively import files from a folder and do calculations with them
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!
Accepted Answer
More Answers (0)
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!