MATLAB Answers

Modify a text file

8 views (last 30 days)
Izem
Izem on 3 Sep 2020
Commented: Izem on 9 Sep 2020
Hello everyone,
I have a .dat file like in the picture and I would like to modify I3 values (the last column). I can't use textscan since the beginning of the file has a different format.
Do you have any idea on how can I do this ?

  3 Comments

Rik
Rik on 3 Sep 2020
There are many methods to read files besides textscan. A simple google search should have turned up several.
What release are you using? Do you want to write the modified data exactly as it is in the original file? Then it might make the most sense to read as text and modify the char array directly.
Izem
Izem on 3 Sep 2020
Thank you sir for your answer !
I am using Matlab 2019b. Yeah I want to keep everything exactely the same except the last column where I want to replace the values by (old values+Delta(m,n)) .
How can I get the char array ?
Izem
Izem on 3 Sep 2020
Sorry for my stupid question. I am new to Matlab.
I think you mean :
C = fileread('text.dat');
Then I can modify those values. Thanks again.

Sign in to comment.

Accepted Answer

Rik
Rik on 3 Sep 2020
You can get my readfile function from the FEX or through the AddOn-manager (R2017a or later).
data=readfile(filename);
HeaderLines=9;
delta=rand(numel(data)-HeaderLines,1);%generate some random data for this example
for n=(HeaderLines+1):numel(data)
%store in temp variable
str=data{n};
%read the value, add something to it, and merge back
ind=strfind(str,' ');ind=ind(end)+1;%assumes you don't have a trailing space and the delimiter is a space
lastval=str2double(str(ind:end));
lastval=lastval+delta(n-HeaderLines);
str=sprintf('%s%.3f',str(1:(ind-1)),lastval);
%store back to array
data{n}=str;
end
%write back to file (overwrites the original)
fid=fopen(filename,'wt');
fprintf(fid,'%s\n',data{:});
fclose(fid)

  17 Comments

Izem
Izem on 9 Sep 2020
Here is what I did and it worked well but is there any bad programming there ?
for i=(HeaderLines+1):numel(data)
%store in temp variable
if isempty(data{i}),continue,end
str=data{i};
vals=textscan(str,' %f %f %f %f %f %f %f');
m=vals{1}; n=vals{2};
for j=1:length(correction)
if m == str2double(mcorrection(j)) && n == str2double(ncorrection(j))
%read the value, add something to it, and merge back
ind=strfind(str,' ');ind=ind(end)+1;%assumes you don't have a trailing space and the delimiter is a space
lastval=str2double(str(ind:end));
lastval=lastval+correction(j);
str=sprintf('%s%.3f',str(1:(ind-1)),lastval);
%store back to array
data{i}=str;
end
end
end
%write back to file (overwrites the original)
fid=fopen(newname,'wt');
fprintf(fid,'%s\n',data{:});
fclose(fid);
Rik
Rik on 9 Sep 2020
I would separate finding the value of the correction and using it. I also would not use i and j as variables. You might also consider using ismember and/or find. A possible optimization would be to convert the mcorrection and ncorrection arrays to double only once (so outside the loop).
So no, I don't see any 'bad programming' here.
Izem
Izem on 9 Sep 2020
Alright! thank you sir for your help, I do appreciate it !

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!