Get "@" back in a table header (R2017b) when saving
이전 댓글 표시
I have got a tabulated data-set which contains "@" in its coloum header (e.g name@domain). When loading this table in Matlab (R2017b) this header gets replaced by name_domain.
I need to do some post-processing on the data and the need the correct header back (i.e. with "@") in the final post processed data when saved as ascii file.
How to achive this in Matlab R2017b?
답변 (2개)
For example,
T=table(1,'VariableNames',"name_domain")
T.Properties.VariableNames = replace(T.Properties.VariableNames,'_','@')
댓글 수: 12
JM
2021년 2월 24일
This should work in 2017b. The number of columns doesn't matter in either case.
T.Properties.VariableNames = strrep(T.Properties.VariableNames,'_','@')
JM
2021년 2월 24일
Steven Lord
2021년 2월 24일
Prior to release R2019b variable names in table and timetable arrays were required to be valid MATLAB identifiers so @ was not allowed in those names. We removed that restriction in release R2019b so if upgrading is an option that would probably be easiest.
I don't think there's going to be an easy way to use writetable to write headers other than those that are already in the table to the file in release R2017b. You may need to post-process the first line of the file to replace _ with @.
JM
2021년 2월 24일
Walter Roberson
2021년 2월 24일
Write a cell row containing the header line. Then write the table with writing of variable names turned off. If you are writing to text then WriteMode append; if you are writing to spreadsheet use a range to avoid writing over the header
JM
2021년 2월 24일
Walter Roberson
2021년 2월 24일
filename = 'YourFile.csv';
outfile = 'post_processed.csv';
T = readtable(filename);
real_names = T.Properties.VariableDescriptions;
data = T{:,:}; %convert to array
%post-process
%done post-process
header = cell2table(real_names);
body = array2table(data);
writetable(header, outfile, 'writevariablenames', false);
writetable(body, outfile, 'writevariablenames', false, 'writemode', 'append');
JM
2021년 2월 24일
Walter Roberson
2021년 2월 24일
filename = 'myData.txt';
outfile = 'post_processed.txt';
T = readtable(filename);
real_names = T.Properties.VariableDescriptions;
%post-process
%at this point use T.VARIABLE or T{:,column} like T.DType or T{:,3}
%and in your processing, update in-place. If you want to create a new
%variable to add, then add its desired output name to the end of real_names
%done post-process
header = cell2table(real_names);
writetable(header, outfile, 'writevariablenames', false);
writetable(T, outfile, 'writevariablenames', false, 'writemode', 'append');
JM
2021년 2월 25일
Walter Roberson
2021년 2월 25일
To be honest, the easiest way would be to upgrade MATLAB releases.
Second easiest way, if you are using MS Windows, would be to generate two seperate files, and then use
system(sprintf('copy "%s"+"%s" "%s"', first_tempfile, second_tempfile, desired_file))
The third easiest way would be to fopen() the file and fprintf() a line at a time, using appropriate formatting.
카테고리
도움말 센터 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!