Creating iteration to make CSVs that do not overwrite

조회 수: 21 (최근 30일)
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
Ive J
Ive J 2021년 1월 12일
편집: Ive J 2021년 1월 12일
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
Jonathan Josephs-Spaulding 2021년 1월 12일
Thank you for this additional feedback and clarification, this makes sense!

댓글을 달려면 로그인하십시오.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by