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 = table
name_domain ___________ 1
T.Properties.VariableNames = replace(T.Properties.VariableNames,'_','@')
T = table
name@domain ___________ 1

12 个评论

Your example does not work in R2017b, so I guess I cannot use this for my issue.
(Also I have a multi-coloumn table)
This should work in 2017b. The number of columns doesn't matter in either case.
T.Properties.VariableNames = strrep(T.Properties.VariableNames,'_','@')
OK. This
T.Properties.VariableNames = strrep(T.Properties.VariableNames,'_','@')
seems to work ( to change from '_' back to '@') until Matlab (2017b) recogniizes that @ cannot be used in a valid variable name. So, I get the following error
forename@surname@domain is not a valid variable name
where name was originally forename_surname. So @ gets in between forename & surname as well.
The updated table (via the above command) does not change T which still has the "_" .
Finally, I want to be able to use
writetable(T,Output_name,'Delimiter','\t')
to write the table as ascii with "@" back in place.
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 @.
OK Thanks.
Is there a way get the text replaced in the final saved table which still has the "_" without using readtable?
If yes, how to find its location in the saved file and swap it with "@"?
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
Thanks @Walter Roberson, Could I have an example? I do not quite follow you.
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');
Thanks. I get an error after running the three lines below using my file 'myDATA.txt'
T = readtable(filename);
real_names = T.Properties.VariableDescriptions;
data = T{:,:}; %convert to array
error:
Cannot concatenate the table variables 'forename_surname' and 'DType', because their types are double and cell.
DType being another coloum header.
Please advice.
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');
Thanks, but it appears writemode is not available in R2017b.
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.

请先登录,再进行评论。

Thanks for all the answers. Upgrading seems the best way out of this but that is not going to happen any time soon.

类别

帮助中心File Exchange 中查找有关 Text Data Preparation 的更多信息

产品

版本

R2017b

标签

提问:

JM
2021-2-23

回答:

JM
2021-2-26

Community Treasure Hunt

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

Start Hunting!

Translated by