How do I read, change and write data back into the same txt file, at the same place?

16 次查看(过去 30 天)
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 个评论
Daniel Price
Daniel Price 2018-8-2
I have made progress with my code... I now am able to write the lines the way I want to. How do I write the lines back into the file where they were before?
clear; clc; close all;
delta = 2.335;
lst = dir('*.txt'); % Collect list of .wsw files to convert
for n = 1:size(lst,1)
t = lst(1).name;
fid = fopen(t);
tline = fgets(fid);
count = 1;
while ischar(tline)
count = count + 1;
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);
tline(15:21)= elev(1:7);
disp(tline);
% fprintf(tline,'%100s', fid(count,:));
else if tline(1) == 'R'& tline(2)== ' '
disp(tline(15:21));
elev = str2num(tline(15:21));
elev = elev + delta;
elev = num2str(elev);
tline(15:21)= elev(1:7);
disp(tline);
% fprintf(tline,'%100s', fid(count,:));
else if tline(1) == 'T'& tline(2)== 'S'
disp(tline(15:21));
elev = str2num(tline(15:21));
elev = elev + delta;
elev = num2str(elev);
tline(15:21)= elev(1:7);
disp(tline);
% fprintf(tline,'%100s', fid(count,:));
else if tline(1) == 'S'& tline(2)== 'H'
disp(tline(15:21));
elev = str2num(tline(15:21));
elev = elev + delta;
elev = num2str(elev);
tline(15:21)= elev(1:7);
disp(tline);
% fprintf(tline,'%100s', fid(count,:));
end
end
end
end
end
end

请先登录,再进行评论。

采纳的回答

Kelly Kearney
Kelly Kearney 2018-8-2
To replace a specific bit of text in a text file, you're going to have to rewrite the entire file. Assuming the files above are representative of your real files, this shouldn't be too time-consuming, because the files are small and easily fit in memory. So I recommend just reading in the entire file at once rather than looping over lines. Using either startsWith (in newer versions of Matlab) or strncmp (in older ones) will be a bit more efficient than your method of comparing individual characters.
The other key to replacing the text with your newly-calculated number is to make sure the new value has the same number of characters as the old one; you can make sure of that by using sprintf instead of num2str when converting your new value into a string.
% Read entire file as character array
txt = fileread(file);
% Split into lines
txt = regexp(txt, '\r\n', 'split');
% Find lines starting with certain letters
istarget = startsWith(txt, 'SO') | ...
startsWith(txt, 'R') | ...
startsWith(txt, 'TS') | ...
startsWith(txt, 'SH');
% Replace value in columns 15-21 with new value, properly formatted
delta = 2.335;
for ii = find(istarget)
elev = str2num(txt{ii}(15:21));
txt{ii}(15:21) = sprintf('%7.3f', elev+delta);
end
% Write to new file
fid = fopen('newfile.txt', 'wt');
fprintf(fid, '%s\n', txt{:});
fclose(fid);
  3 个评论
Thiago de Aquino Costa Sousa
Dear community,
I have many txt files that uses a tab delimiter. I have to change the content between the 17th and 18th tab delimiter of all my files. This is one the data inside the file:
_______
Data file generated on Tue Aug 30 12:40:29 2022
A B C D E F G H I J K L M N O P Q R S T U
1 2.00000 2.00000 0.00000 1.00000 28.00000 64.00000 88.00000 1.20000 275.00000 50.00000 30.00000 165.00000 850.00000 500.00000 0.00000 10.00000 10.00000 15.00000 0,0,0,0, 0,0,0,0, false
_______
So I have to change the value for the variable Q (in this case 10.0000) for a specific value. However, some of my files have a different pattern for numbers, instead of 10.00000 it is only 10, what makes the counting characters unfeasible. Furthermore, after some delimiters there are spaces. Then, I decided to count the tab delimeters, and change the content between the 17th and 18th delimiter to solve my problem, that I don't know if it will work. This is my code so far...
ThemeCopy
A = regexp(fileread(myfile), '\n', 'split'); %upload my file to a cell array
B = strfind(A{4}, sprintf('\t')); %takes the cell 4 in the array and finds where I have tab delimiters
C = B{4}(17)+1:B{4}(18)-1; %select the content between the 17th and 18th tab delimiters
Please, can somebody help me???

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by