Creating iteration to make CSVs that do not overwrite

10 vues (au cours des 30 derniers jours)
Jonathan Josephs-Spaulding
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 commentaires
Ive J
Ive J le 12 Jan 2021
Modifié(e) : Ive J le 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
Thank you for this additional feedback and clarification, this makes sense!

Connectez-vous pour commenter.

Réponse acceptée

J. Alex Lee
J. Alex Lee le 12 Jan 2021
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
  1 commentaire
Jonathan Josephs-Spaulding
A very nice solution and alternative, thank you for adding this !

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by