Edit a column in a dat file with some header lines and a table
5 views (last 30 days)
Show older comments
I want to change BlTwist column in the attached file by adding a constant floating or double number to each element of this column with the header lines preserved in the file. I have tried readtable and writetable functions and successfully changed the mentioned column but header lines vanish in the edited file. Only table remains with the changed column. I need header lines too. I couldn't upload the dat file so I converted it into a text file. Orignially, it has to have a dat extension.
3 Comments
ahmad hassan
on 9 Mar 2022
Hi Hashir,
I am searching solution for exact same problem. Did you find something about it?
Answers (1)
dpb
on 15 Dec 2020
Edited: dpb
on 15 Dec 2020
A) goes something like
nHeaders=11; % or whatever
fidi=fopen(filenameIn,'r');
fido=fopen(filenameOut,'w');
for i=1:nHeaders
fprintf(fido('%s',fegts(fidi)))
end
data=textscan(fido(fmtInData));
...
%manipulate the data array here
fprintf(fido,fmtOut,data.')
fidi=fclose(fidi);
fido=fclose(fido);
B) would just
nHeaders=11;
data=importdata(filenameIn); % will return cellstr() array
values=char(data(nHeaders+1:end)); % pick the data rows; convert to char() array
values=str2double(values); % and convert to numeric
%manipulate the data as needed here
fido=fopen(filenameOut,'w');
for i=1:nHeaders
fprintf(fido('%s',data{i})
end
fprintf(fido,fmtOut,values.')
fidi=fclose(fidi);
fido=fclose(fido);
You'll have to clean up the details and all, but gives the general sequence of operations. What you do with the embedded remark line in the file is going to be a tussle unless you can just ignore it with the 'CommentSyle' parameter with textscan on input. Otherwise, if you must echo it back out you'll have to parse the file as string data and locate it--the numeric conversion/reading routines will barf on it.
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!