This works. The example input and output files are attached.
newData = [1,2;3,4;5,6;7,8];
newName = 'blah.prw';
fid = fopen('oldfile.txt', 'rt');
fod = fopen('newfile.txt', 'wt');
for k = 1:3
fprintf(fod,'%s\n',fgetl(fid));
end
str = regexprep(fgetl(fid),'\w+\.prw',newName);
fprintf(fod,'%s\n',str)
cmp = ' !-';
while ~isequal(intersect(str,cmp),cmp)
str = fgetl(fid);
fprintf(fod,'%s\n',str)
end
fprintf(fod,' %.7f %.7f\n',newData.')
while ~isempty(str)
str = strtrim(fgetl(fid))
end
fprintf(fod,' \n')
while ~feof(fid)
fprintf(fod,'%s\n',fgetl(fid))
end
fclose(fid);
fclose(fod);
Best Answer