Effacer les filtres
Effacer les filtres

Do calculations in csv data one csv file at the time

3 vues (au cours des 30 derniers jours)
Martin Kneyber
Martin Kneyber le 27 Avr 2024
Modifié(e) : Voss le 28 Avr 2024
I have several csv files. Want to perform calculations on the data in the csv file, for example calculating variable x. Then, I want one table with in the first column the filename of each csv file and then in the next column the calculated variable x. How can I do this? Newbie,so sorry for asking.
  3 commentaires
Martin Kneyber
Martin Kneyber le 27 Avr 2024
Sure, but there are a lot of files and each files has a big number of rows - hence looking for a way to automate it
Dyuman Joshi
Dyuman Joshi le 27 Avr 2024
Use readtable or readmatrix to import the data, and their counter-parts writetable or writematrix to export the data.

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 27 Avr 2024
Something like this; adjust as necessary.
% use dir() to get info about the relevant csv files:
csv_dir = '.';
F = dir(fullfile(csv_dir,'*.csv'));
filename = fullfile({F.folder},{F.name}).';
% preallocate x. I assume it's a scalar numeric for each file
N = numel(F);
x = zeros(N,1);
% loop over the files
for ii = 1:N
% read each file (use the appropriate function with the appropriate options)
data = readtable(filename{ii});
% ...
% calculate this_x based on data
% ...
% store this_x
x(ii) = this_x;
end
% write file with file names and x values
T = table(filename,x);
writetable(T,'out.csv')
  2 commentaires
Martin Kneyber
Martin Kneyber le 28 Avr 2024
Thank you !!!!!
Voss
Voss le 28 Avr 2024
Modifié(e) : Voss le 28 Avr 2024
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 27 Avr 2024
See the FAQ for code samples to process a sequence of files:

Catégories

En savoir plus sur Data Import and Analysis dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by