2d to 3d array conversion

1 visualización (últimos 30 días)
J Sujatha
J Sujatha el 26 de Sept. de 2020
Comentada: Ameer Hamza el 30 de Sept. de 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.

Respuestas (1)

Ameer Hamza
Ameer Hamza el 26 de Sept. de 2020
Editada: Ameer Hamza el 26 de Sept. de 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 comentarios
J Sujatha
J Sujatha el 30 de Sept. de 2020
It's completely working! Thanks a lot!!
Ameer Hamza
Ameer Hamza el 30 de Sept. de 2020
I am glad to be of help!!!

Iniciar sesión para comentar.

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by