Creating iteration to make CSVs that do not overwrite

21 ビュー (過去 30 日間)
Jonathan Josephs-Spaulding
Jonathan Josephs-Spaulding 2021 年 1 月 11 日
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 Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by