Creating iteration to make CSVs that do not overwrite
Show older comments
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
Jonathan Josephs-Spaulding
on 12 Jan 2021
Edited: Jonathan Josephs-Spaulding
on 12 Jan 2021
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.
Jonathan Josephs-Spaulding
on 12 Jan 2021
Accepted Answer
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!