How do I read, change and write data back into the same txt file, at the same place?
Show older comments
I have many files in one folder like the two examples attached. I want to change the number formed in columns 14-21 of any row that starts with "SO" "R " "TS" or "SH" by "2.335". Then write the numbers back to the row in the same file. Below is what I've tried so far:
clear; clc; close all;
delta = 2.335;% delta is the change in elevation
lst = dir('*.txt');
for n = 1:size(lst,1)
t = lst(1).name;
fid = fopen(t);
tline = fgets(fid);
while ischar(tline)
tline = fgetl(fid);
if tline(1) == 'S'& tline(2)== 'O'
disp(tline(15:21));
elev = str2num(tline(15:21));
elev = elev + delta;
elev = num2str(elev)
end
end
end
Thank you.
1 Comment
Daniel Price
on 2 Aug 2018
Accepted Answer
More Answers (0)
Categories
Find more on Entering Commands 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!