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개)

Matt J
Matt J 2021년 2월 23일
편집: Matt J 2021년 2월 23일
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

JM
JM 2021년 2월 24일
Your example does not work in R2017b, so I guess I cannot use this for my issue.
(Also I have a multi-coloumn table)
Matt J
Matt J 2021년 2월 24일
편집: Matt J 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,'_','@')
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 @.
JM
JM 2021년 2월 24일
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
JM
JM 2021년 2월 24일
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');
JM
JM 2021년 2월 25일
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.

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

JM
JM 2021년 2월 26일

0 개 추천

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