How to dynamically name a large number of tall tables?
3 次查看(过去 30 天)
显示 更早的评论
Before anyone jumps in and says it, I've read through all the responses that say why you shouldn't dynamically name tables in Matlab.
I have about 2,000 csv files, each with over 1m records, and I need to break out the records from each file into individuals tables based on their categorical name in a column of each csv file (there are about 6,000 different categorical names). I would just like to name the table based on the categorical name.
If I try breaking them up into structure fields, the structure quickly becomes overloaded, and it literally takes hours to load / save the structure, and ages to work with the structure, which is not realistic when I really just want to work with each category at a time.
I wanted to try to use tall tables, because once I organize these files, some tables should have a couple million rows, while others may have a couple thousand.
Please let me know if you have any suggestions on efficiently breaking up the data into individual tables and dynamically naming those tables, and I'm going to ignore any comments about how that route is just for beginners that don't know anything about coding.
3 个评论
David Hill
2022-11-1
Are all the names of the csv files similar? Provide and example. Are you just using readtable() to import the csv files?
采纳的回答
David Hill
2022-11-1
This should read all tables one at a time, combine the same categorical names, and store new tables as the categorical names.
d=dir('*.csv');%assuming all files are together in the same folder with nothing else
s=size(d,1);
for k=1:s
r=readtable(d(k).name);
catnams=r.Properties.VariableNames;
for m=1:length(catnams)
try
R=readtable([catnams{m},'.csv']);
R=[R(:,1);r(:,m)];
writetable(R,[catnams{m},'csv']);%not sure if you want to save as .mat files or .csv
catch
R=r(:,m);
writetable(R,[catnams{m},'csv']);%not sure if you want to save as .mat files or .csv
end
end
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Tall Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!