我需要在Matlab中替换文本文件中的特定字符串并将其保存在新的文本文件中。这是我的代码,它显示了命令窗口中的更正替换,但新文件是相同的。
fin = fopen('mooj1.txt');
fout = fopen('copy of mooj1.txt');
while ~feof(fin)
s = fgetl(fin);
s = strrep(s, '57.0833 30.2833', '53.0833 -28.2833');
fprintf(fout,'%s',s);
disp(s)
end
fclose(fin);
fclose(fout);
答案 0 :(得分:1)
dlmwrite('copy of mooj1.txt',s,'delimiter',' ','precision',12);
你将在txt中保存数组“s”
fin = fopen('mooj1.txt');
while ~feof(fin)
s = fgetl(fin);
s = [s, ' 57.0833 30.2833 ', ' 53.0833 -28.2833 '];
disp(s)
end
fclose(fin);
dlmwrite('copy of mooj1.txt',s,'delimiter','','precision',12);
答案 1 :(得分:1)
fout = fopen('copy of mooj1.txt','w');
使用'w'
(在代码的第二行)
完整代码:
fin = fopen('mooj1.txt');
fout = fopen('copy of mooj1.txt','w');
while ~feof(fin)
s = fgetl(fin);
s = strrep(s, '57.0833 30.2833', '53.0833 -28.2833');
fprintf(fout,'%s',s);
disp(s)
end
fclose(fin);
fclose(fout);
希望这有帮助!