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!