【发布时间】:2019-10-24 21:50:11
【问题描述】:
我有一个如下所示的文本文件(例如:data.dat),其中包含许多行。
data.dat
@motion parameters
speed= 22,30,60
range= 600
rotation= 50
@controls
act= 2,3,4,5
我想阅读它并替换以特定关键字开头的行之后的行,例如“@控件”。在这种情况下,要替换的行是这一行:
act= 2,3,4,5
它应该在一个循环中改变。例如,它会瞬间变为:
act= 1,0,8,-2
我试过了
fullFileName = fullfile(pwd, 'data.dat')
% Open the file.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
while ischar(textLine)
% Read the remaining lines of the file.
fprintf('%s\n', textLine)
if startsWith(textLine,'@controls')
% Line starts with @controls, so change values
textLine = fgetl(fileID); % Step one line below
textLine = 'act= %4.1f,%4.1f,%4.1f,%4.1f\n ';
fprintf(fileID,textLine,act_1,act_2,act_3,act_4);
end
% Read the next line.
textLine = fgetl(fileID);
end
% All done reading all lines, so close the file.
fclose(fileID);
但这只是删除了原始行!
感谢您的帮助。提前致谢。
【问题讨论】:
-
你累了什么?
-
检查我的编辑..!
标签: matlab file text text-files