How to export a table including column names?

37 visualizaciones (últimos 30 días)
Lu Da Silva
Lu Da Silva el 20 de Mzo. de 2022
Comentada: Star Strider el 7 de Abr. de 2023
I have a matrix A that I want to convert into a table with variable names and export as csv.
This is my code:
MyTable = array2table(A);
MyTable.Properties.VariableNames(1:4) = {'Min','Max', 'Mean', 'St. Dev.'};
writetable(MyTable,'MyTable.csv', 'WriteVariableNames', true);
But I get the following error:
Error using writetable
Unsupported type 'double'. Use writematrix instead.
But when using 'writematrix' the column names aren't supported.
NB: one of the columns in A has NaN in some rows.
Update: the code above works fine; there was a mistake in the original code. SORRY!

Respuesta aceptada

Jan
Jan el 20 de Mzo. de 2022
Maybe there is a typo in the original code? If I adjust the variable names, which must be legal Matlab symbols in my R2018b version, it is running:
A = rand(5, 4); % Some test data
MyTable = array2table(A);
MyTable.Properties.VariableNames(1:4) = {'Min','Max', 'Mean', 'StDev'};
writetable(MyTable,'MyTable.csv', 'WriteVariableNames', true);
  1 comentario
Lu Da Silva
Lu Da Silva el 20 de Mzo. de 2022
Editada: Lu Da Silva el 20 de Mzo. de 2022
You're right, there was a typo! My bad, so sorry! But thank you!

Iniciar sesión para comentar.

Más respuestas (1)

Star Strider
Star Strider el 20 de Mzo. de 2022
It might be better to write it as a text file instead, since a .csv file may not be appropriate.
writetable(MyTable,'MyTable.txt', 'WriteVariableNames', true);
  6 comentarios
Paul Safier
Paul Safier el 6 de Abr. de 2023
Thanks @Star Strider that solved my problem too!
Star Strider
Star Strider el 7 de Abr. de 2023
@Paul Safier — My pleasure!
A Vote would be appreciated!

Iniciar sesión para comentar.

Categorías

Más información sobre Structures 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!

Translated by