Using writetable in a for loop to create new txt/csv files for each step

Hi,
I am basically going through csv files and adding a collumn on which is the running cumlitive sum of 144 entries.
I then want to write a new vairable for each table. I have been using the writetable function but can not seem to get it working with errors regarding the filename. Below is my code without the writetable line, could anyone assist with what I should write to export each iteration as a csv or txt file
% Get the list of all .mat files in current directory
f=dir('*.csv');
for i=1:length(f)
% Read the input file name one-by-one. Load variables to 's'
% It's a structure NOTE to self
x=readtable(f(i).name);
% Get the field names of above structure
fld=fieldnames(x);
% Read actual table
t=x.(fld{1});
% This loop creates a range of rows to be filtered out
for j=1:height(x)
%%add collumn for cumulitve sum
x.AOI=movsum(x.exceedConst,144);
end
end

4 commentaires

The above code makes no sense -- readtable will return a table object, not a struct
To make any sense of it, attach one or two of the .csv files -- use the paperclip icon.
the above code I had edited an old script, but I have reviewed it and it can be done much simpler. I have attached one of 200 csv fies that the script below reads;
% Get the list of all .mat files in current directory
f=dir('*.csv');
for i=1:length(f)
% Read the input file name one-by-one. Load variables to 's'
x=readtable(f(i).name);
%%add collumn for cumulitve sum
x.AOI=movsum(x.exceedConst,144);
end
As above I want to use writetable to save each iteration
% if you wanna write the 'whole' modified table to a new file
for i = 1:numel(f)
% do whatever
newfile = ['file', num2str(i), '.csv'];
writetable(x, newfile);
end
% or if you wanna write only 'AOI' to a new file
for i = 1:numel(f)
% do whatever
newfile = ['file', num2str(i), '.csv'];
writetable(x(:, end), newfile);
end
Thanks, great help worked perfectly

Connectez-vous pour commenter.

 Réponse acceptée

Scott MacKenzie
Scott MacKenzie le 23 Juil 2021
Modifié(e) : Scott MacKenzie le 23 Juil 2021
Under the assumption your code is working fine except for the filename issue, here's what I suggest. Use this to create a new filename for the output file. Add the line just before writing to the file:
fNew = insertBefore(f(i).name, '.csv', strcat('_', string(i)));
fNew is the name of the file to write to via writetable. It's the name of the file being processed except adding '_n' just before '.csv'. The value of n is the value of i in the loop.

Plus de réponses (1)

hello
seems you were one inch or even less to a solution .
Now it was a bit unclear to me if you wanted to save the new table in the original file or to another file
basically the two options are available here :
clc
clearvars
% Get the list of all .mat files in current directory
f=dir('Alarm*.csv');
for i=1:length(f)
% Read the input file name one-by-one. Load variables to 's'
x=readtable(f(i).name);
%%add collumn for cumulitve sum
x.AOI=movsum(x.exceedConst,144);
% write new table in output file
% writetable(x,['out' num2str(i) '.csv']); % debug line : to not
% overwrite input data file => saved in different file name
writetable(x,f(i).name); % this will overwrite input data file
end

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by