How to execute the same operation on various .csv files?
Mostrar comentarios más antiguos
I have a list (62 items) of .csv files, all within a single folder. My goal would be to make the same operation on all of these files; I managed to write the code on a single .csv file, which is as follows (actually it is a trapz operation but written extensively):
%my code on a single .csv file
T = readtable('M_20210325-1519_90_l90_.csv','HeaderLines', 11); %read single .csf file and skip 11 lines
[numRows,numCols] = size(T);
cont=0;
tB= T(:,3); %consider 3rd column
B=table2array(tB) %consider it as an array
bvB=B(1,1); %consider the first value
i=1;
intB=zeros(numRows,1); % create an array full of zeros
newB=B(:,1)-bvB; %subtract the first value
z=numRows-1;
for cont=1:z
if (newB(i+1,1)+newB(i,1))>=0
intB(i,1)= (newB(i+1,1)+newB(i,1))/2;
else
intB(i,1)=0;
end
dbstop if error;
i=i+1;
end
sommaB=sum(intB); %useful result
I had been looking for some scripts that allow to consider and read all the data from the .csv files, such as
files = dir('*.csv')
N = length(files) ;
% loop for each file
fileNames = {files.name};
for k = 1:N
data{k} = csvread(fileNames{k});
%%do whatever you want
end
(which gives the Trouble reading 'Numeric' field from file error, because I guess it does not skip the header lines), or
fds = fileDatastore('*.csv', 'ReadFcn', @importdata)
fullFileNames = fds.Files
numFiles = length(fullFileNames)
% Loop over all files reading them in and plotting them.
for k = 1 : numFiles
fprintf('Now reading file %s\n', fullFileNames{k});
% do the operation --------> my single operation
end
Both are useful because I get all the .csv files, already sorted in a proper way, but I'm missing the link between considering them one at a time and execute the operation. Probably it's relatively easy, but I'm a beginner.
I attach two .csv files as an example.
Thank you
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Workspace Variables and MAT Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!