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

2 次查看(过去 30 天)
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 个评论
Hashir Shafi
Hashir Shafi 2020-12-15
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 2022-3-9
Hi Hashir,
I am searching solution for exact same problem. Did you find something about it?

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2020-12-15
编辑:dpb 2020-12-15
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 个评论
Hashir Shafi
Hashir Shafi 2020-12-16
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by