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

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Hashir Shafi
Hashir Shafi il 15 Dic 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 il 9 Mar 2022
Hi Hashir,
I am searching solution for exact same problem. Did you find something about it?

Accedi per commentare.

Risposte (1)

dpb
dpb il 15 Dic 2020
Modificato: dpb il 15 Dic 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 Commento
Hashir Shafi
Hashir Shafi il 16 Dic 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.

Accedi per commentare.

Categorie

Scopri di più su Text Data Preparation in Help Center e File Exchange

Prodotti


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by