필터 지우기
필터 지우기

how to preserve original column headings (variable names) when using writetable

조회 수: 395 (최근 30일)
When importing spreadsheets containing invalid variable names, Matlab changes them and stores the original column headings in the VariableDescriptions property of the table. How can I write my table to a file and use the original variable names on export? In other words, how can I export T so that output_table.xlsx is the same as input_table.xlsx?
Reprocessing the exported file as a string is not really an option, because my original file will have multiple sheets.
T = readtable('input_table.xlsx'); % Matlab changes "Column 1" to "Column1"
writetable(T,'output_table.xlsx');

채택된 답변

Sindar
Sindar 2020년 4월 22일
As of 2019b, table variable names don't need to follow normal variable naming rules. This allows for a couple of fixes:
T = readtable('input_table.xlsx','PreserveVariableNames',true);
or
T.Properties.VariableNames={'Column 1';'Column 2'}
  댓글 수: 2
fixusc
fixusc 2020년 4월 22일
Wow, thanks for pointing this out.
It's however very likely to cause problems when writing code for someone else because it is hard to assure that everybody has at least 2019b. But cool to know for my own stuff.
Alan Pawollek
Alan Pawollek 2021년 10월 15일
편집: Alan Pawollek 2021년 10월 15일
Thanks @Sindar works perfecty fine :) Now I don't need to do it via
[name of File].Properties.VariableNames
and write it manually for each coloumn.
ps. I habe the latest version of Matlab R2021b

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by