2d to 3d array conversion

3 vues (au cours des 30 derniers jours)
J Sujatha
J Sujatha le 26 Sep 2020
Commenté : Ameer Hamza le 30 Sep 2020
I have 14641 csv files each of dimension 257x7. I need to convert it to a 3d array of dimension 257x7x14641.
I generated an array of 1x14641 dimension each element having 257x7 dimension by the following code snippet:
for i=1:14641
a{i} = readtable(strcat('output_',num2str(i),'.csv'));
end
I thought of trying 'presume' and 'reshape' functions. They are throwing an error saying "Cell contents reference from a non-cell array object".
Then, I tried 'cat' function. It is throwing error "Duplicate variable name: alpha" as I have column headings in every csv file.
Should I continue this approach? How to overcome there errors? Or is there an alternative approach?
P.S.: A sample csv file is attached.

Réponses (1)

Ameer Hamza
Ameer Hamza le 26 Sep 2020
Modifié(e) : Ameer Hamza le 26 Sep 2020
First convert tables to arrays and then use cat()
for i=1:14641
a{i} = readtable(strcat('output_',num2str(i),'.csv'));
end
a = cellfun(@table2array, a, 'UniformOutput', false);
M = cat(3, a{:}); % concatenate in 3rd dimension
Or newer vesrions, you can directly use readmatrix and then use cat()
for i=1:14641
a{i} = readmatrix(strcat('output_',num2str(i),'.csv'));
end
M = cat(3, a{:}); % concatenate in 3rd dimension
  6 commentaires
J Sujatha
J Sujatha le 30 Sep 2020
It's completely working! Thanks a lot!!
Ameer Hamza
Ameer Hamza le 30 Sep 2020
I am glad to be of help!!!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by