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
댓글 수: 2
추가 답변 (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!