Adding column headers to writetable csv file

I'm trying to figure out how to add column headers to my csv files using writetable.
I started with creating a table of variables (transposed into columns) and then used writetable to export it as a csv. Right now the headersin the output file are Var1 and Var2. How can I change this?
T6 = table(average_in_bin.',num_per_bin.');
writetable(T6,'avg_num_per_bin.csv')

 채택된 답변

Adam Danz
Adam Danz 2019년 4월 29일
편집: Adam Danz 2021년 9월 13일

2 개 추천

The default value for the WriteVariableNames parameter in writetable() is true so all you need is to add variable names to your 'T6' table. Below I named them "NameOne" and "NameTwo".
T6 = table(average_in_bin.',num_per_bin.', 'VariableNames', {'NameOne', 'NameTwo'});
writetable(T6, 'avg_num_per_bin.csv', 'WriteVariableNames', true)

댓글 수: 3

Jurgen vL
Jurgen vL 2019년 9월 23일
편집: Jurgen vL 2019년 9월 23일
Note that table variables still have to comply with the character restrictions for MATLAB variable names. So if you want more flexibility in your exported table names, consider using writecell(). Although softwares like R handle names with hyphens and spaces and dots, best-practices include camelCase and underscores.
I believe the "True" has to be lowercase.
writetable(T6, 'avg_num_per_bin.csv', 'WriteVariableNames', true)
Adam Danz
Adam Danz 2021년 9월 13일
Yep, thanks. Corrected.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB Report Generator에 대해 자세히 알아보기

태그

질문:

2019년 4월 29일

댓글:

2021년 9월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by