方法一:使用fscanf函数,但会出现问题:读入的数据类型必须相同,否则不能读入。
方法二:按行读取按行修改,可以完成任务要求,缺点是相对费时。以下介绍具体步骤。
(1)以读的形式打开文本:fopen
(2)按行读取文本内容:fgetl
(3)将行内容存储在数组(?结构体?)中,其元素为字符串。
(4)修改行内容:strrep
修改时需要进行数据类型转换:str2num、num2str
(5)关闭该文本:fclose
(6)以写的形式再次打开文本:fopen
(7)写文本:fprintf
(8)关闭该文本:fclose
clc
clear all
close all
%读并修改文本
fidin1=fopen('C:\Users\dell\Desktop\infrared\cloud\results3.txt','r+');
i=0;
while ~feof(fidin1)
tline=fgetl(fidin1);
i=i+1;
newtline{i}=tline;
newtline{i}=strrep(tline,tline(1:6),num2str(str2num(tline(1:6))-100075));
end
fclose(fidin1);
%写文本
fidin1=fopen('C:\Users\dell\Desktop\infrared\cloud\results3.txt','w+');
for j=1:1:i
fprintf(fidin1,'%s\r\n',newtline{j});
end
fclose(fidin1);
参考链接:
https://zhidao.baidu.com/question/368142519987390604.html
https://blog.csdn.net/joris30/article/details/88891341