Creating iteration to make CSVs that do not overwrite

Dear MATLAB community,
I recently started to learn MATLAB and would like some support in how to produce multiple non-overwriting CSVs with a loop. I would like to learn the difference between commands that reiterate and overwrite (which I presently have), in comparison to a script which produces unique files.
I wrote a simple script to first load all cell arrays in my directory:
files = dir('/Fastcore/Stem/*.mat');
N=length(files);
Data=cell(1,N);
for i=1:N
Data{1,i} = (sprintf('%s%s','/Fastcore/Stem/', files(i).name));
end
Next I loop the writing of the variables from my loaded data. These outputs overwrite each other, however I cannot figure out how to produce N csvs into my directory without overwriting:
for i=1:N
load(Data{i});
writetable(cell2table(tissueModel(1).rxns),'/Fastcore/Test/RxnsNames.csv');
end
Any feedback or suggestions would be appreciated!!!
Cheers,
~Jonathan J.S

4 Comments

I don't understand the relation between indices and what you're actually writing to the output (tissueModel). Regardless, you are not changing the name of output files, so all the data within the loop are written to the same output: RxnsNames.csv
for i=1:N
indices = load(Data{i});
writetable(cell2table(tissueModel(1).rxns), "/Fastcore/Test/RxnsNames" + i + ".csv");
end
Thank you kindly Ive, this does work. "Indices" is irrelevant so I removed this and edited my question. I am just curious and have a follow-up questions: what does
"+ i "
do within
cell2table ?
Thank you again for your healp!
~Jonathan J.S
It has nothing to do with cell2table but is the argument of readtable. You simply create dynamic file names within the loop:
i = (1:5)';
myfileNames = "myFile" + i + ".csv"
5×1 string array
"myFile1.csv"
"myFile2.csv"
"myFile3.csv"
"myFile4.csv"
"myFile5.csv"
So, you write each data within loop to a distinct csv file.
Thank you for this additional feedback and clarification, this makes sense!

Sign in to comment.

 Accepted Answer

I agree with Ive J and their solution will work, but if N>9, you may want to include leading zeros. I'm not sure how to do that with the string addition operators, but you can also use the sprintf mechanism you already know about
csvpath = sprintf('/Fastcore/Test/RxnsNames-%03d.csv',i)
Alternatively, you maybe you want to preserve the original mat file's base name.
files = dir('/Fastcore/Stem/*.mat');
% one loop
for i = 1:length(files)
% choose one of the csv naming schemes
[~,base] = fileparts(files(i).name);
csvpath = fullfile(files(i).folder,sprintf("%s.csv",base))
% % alternative csv naming
% csvpath = fullfile(files(i).folder,sprintf("RxnsNames%03d.csv",i));
% load mat file into structure to avoid ambiguity pointed out by Ive J
matpath = fullfile(files(i).folder,files(i).name);
celldata = load(matpath)
% you don't necessarily need this intermediary, but in case you want to do further post-processing later
tabledata = cell2table(celldata.tissueModel(1).rxns);
% now you don't have to modify this part if you change you mind about anything above
writetable(tabledata,csvpath)
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!