How to export a table including column names?
이전 댓글 표시
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!
채택된 답변
추가 답변 (1개)
Star Strider
2022년 3월 20일
2 개 추천
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
Lu Da Silva
2022년 3월 20일
It works for me, including NaNs:
T = array2table([1,2;NaN,3;NaN,4;5,6]);
T.Properties.VariableNames = {'X','Y'};
writetable(T,'test.txt')
type test.txt
Lu Da Silva
2022년 3월 20일
Star Strider
2022년 3월 20일
@Stephen — Thank you!
Paul Safier
2023년 4월 6일
Star Strider
2023년 4월 7일
@Paul Safier — My pleasure!
A Vote would be appreciated!
카테고리
도움말 센터 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!