- Loading txt into a cell.
- Then do whatever you want with the cell. In this example, line 69 will be 99.
- Then write the cell into a txt.
How to change a specific line in a text file?
159 ビュー (過去 30 日間)
古いコメントを表示
Sebastian
2013 年 2 月 11 日
コメント済み: Thiago de Aquino Costa Sousa
2022 年 10 月 2 日
Hi
I have a text file, where different forms of content exist, data or strings or symbols, i need to change a specific line (it's only a number), i know its position (69 line)
Is there any way to do so?
0 件のコメント
採用された回答
ChristianW
2013 年 2 月 11 日
編集済み: ChristianW
2013 年 2 月 11 日
% Read txt into cell A
fid = fopen('test.txt','r');
i = 1;
tline = fgetl(fid);
A{i} = tline;
while ischar(tline)
i = i+1;
tline = fgetl(fid);
A{i} = tline;
end
fclose(fid);
% Change cell A
A{69} = sprintf('%d',99);
% Write cell A into txt
fid = fopen('test2.txt', 'w');
for i = 1:numel(A)
if A{i+1} == -1
fprintf(fid,'%s', A{i});
break
else
fprintf(fid,'%s\n', A{i});
end
end
5 件のコメント
Thiago de Aquino Costa Sousa
2022 年 10 月 2 日
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.
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???
その他の回答 (2 件)
Abdul
2015 年 8 月 11 日
編集済み: Walter Roberson
2018 年 7 月 17 日
I have written a function that scans for the specified SearchString in the InputFile and replaces it with the ReplaceString in the OutputFile. If the same file is specified as both the input and output file, the file is overwritten.
function [] = func_replace_string(InputFile, OutputFile, SearchString, ReplaceString)
%%change data [e.g. initial conditions] in model file
% InputFile - string
% OutputFile - string
% SearchString - string
% ReplaceString - string
% read whole model file data into cell array
fid = fopen(InputFile);
data = textscan(fid, '%s', 'Delimiter', '\n', 'CollectOutput', true);
fclose(fid);
% modify the cell array
% find the position where changes need to be applied and insert new data
for I = 1:length(data{1})
tf = strcmp(data{1}{I}, SearchString); % search for this string in the array
if tf == 1
data{1}{I} = ReplaceString; % replace with this string
end
end
% write the modified cell array into the text file
fid = fopen(OutputFile, 'w');
for I = 1:length(data{1})
fprintf(fid, '%s\n', char(data{1}{I}));
end
fclose(fid);
7 件のコメント
Walter Roberson
2022 年 3 月 3 日
data{1}{I} = regexprep(ReplaceString, '\\n', '\n'); % replace with this string
Walter Roberson
2013 年 2 月 11 日
sed. Or perl. Or python.
Or you could open the input file, open the output file, copy 68 lines from the input to the output, then read one line from input and write out the replacement output instead of the input line, then read the rest of the lines from input and copy them to output; finally, close both files.
There are fancier ways using (e.g.,) textscan() to read a bunch of lines at once.
Really, though, if you are on Linux or OS-X, I recommend using the system utility sed.
system('sed -e "69s/.*/31419/" < OLDFILE > NEWFILE')
here the 69 is the line you want changed and the 31419 is the new value you want put in
1 件のコメント
Walter Roberson
2021 年 3 月 12 日
These days, MATLAB also supports
readlines(filename)
which reads the file into a cell array of character vectors, one per line. You can then make whatever changes to the line (using {} indexing) and write the lines out again. For example,
S = readlines('test.txt');
S{69} = regexprep(S{69}, '-1', '31419'); %file says -1 change it to 31419
[fid, msg] = fopen('new_test.txt', 'w');
if fid < 1;
error('could not write output file because "%s"', msg);
end
fwrite(fid, strjoin(S, '\n'));
fclose(fid);
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!