2d to 3d array conversion

1 visualizzazione (ultimi 30 giorni)
J Sujatha
J Sujatha il 26 Set 2020
Commentato: Ameer Hamza il 30 Set 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.

Risposte (1)

Ameer Hamza
Ameer Hamza il 26 Set 2020
Modificato: Ameer Hamza il 26 Set 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 Commenti
J Sujatha
J Sujatha il 30 Set 2020
It's completely working! Thanks a lot!!
Ameer Hamza
Ameer Hamza il 30 Set 2020
I am glad to be of help!!!

Accedi per commentare.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Prodotti


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by