How to dynamically name a large number of tall tables?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hayley Rogers
el 1 de Nov. de 2022
Editada: Hayley Rogers
el 2 de Nov. de 2022
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 comentarios
David Hill
el 1 de Nov. de 2022
Are all the names of the csv files similar? Provide and example. Are you just using readtable() to import the csv files?
Respuesta aceptada
David Hill
el 1 de Nov. de 2022
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Tall Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!