Edit a column in a dat file with some header lines and a table

5 views (last 30 days)
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
Hashir Shafi
Hashir Shafi on 15 Dec 2020
Thanks for your comment. I have tried reading and copying the headerlines as text but then I don't know how to append the file with the manipulated table. And about second option, are you talking about storing all the content of the file in a cell array in workspace and then manipulating it? If yes, can you please give me an idea of how would you do it? Because I have tried doing it but couldn't succeed.
ahmad hassan
ahmad hassan on 9 Mar 2022
Hi Hashir,
I am searching solution for exact same problem. Did you find something about it?

Sign in to comment.

Answers (1)

dpb
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.
  1 Comment
Hashir Shafi
Hashir Shafi on 16 Dec 2020
Thanks for your input. I have already managed to write header lines in the new file. I am stuck at the manipulation part but I am working on it. Hopefully, it will be resolved soon.

Sign in to comment.

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!