【发布时间】:2014-07-05 03:05:58
【问题描述】:
我正在编写一个生成数组编号的 Matlab 代码,它应该替换文本文件中的每个数字(已经存在)并用它替换所有实例。该数字应为字符串格式。我已经做到了:
ita='"';
for i=1:size(z,2)
word_to_replace=input('Replace? ','s');
tik=input('Replacement? ','s');
coluna=input('Column? ');
files = dir('*.txt');
for i = 1:numel(files)
if ~files(i).isdir % make sure it is not a directory
contents = fileread(files(i).name);
fh = fopen(files(i).name,'w');
val=num2str(z(i,coluna));
word_replacement=strcat(tik,val,ita);
contents = regexprep(contents,'word_to_replace','word_replacement');
fprintf(fh,contents); % write "replaced" string to file
fclose(fh) % close out file
end
end
end
我希望代码打开文件#1 ('file.txt'),找到所有实例 'word_replacement' 并将其替换为 'word_to_replace' 并保存到同一个文件。 txt文件个数未定义,可以是100个,也可以是10000个。
非常感谢。
【问题讨论】: