filename = 'AppropriateFile.txt';
outfilename = 'Revised_File.txt';
LineToChange = 65;
NewContent = 'B 50';
S = fileread(filename);
SS = regexp(S, '\r?\n', 'split');
SS{LineToChange} = NewContent;
fid = fopen(outfilename, 'w');
fprintf(fid, '%s\n', SS{:});
fclose(fid);
It is not usually recommended ot make the output file name the same as the input file name: if you do that and there is a problem with the change, you could end up with the content of the file destroyed but the revised content not written into place. For example if there were an error in the fprintf() line, then the fopen() line right before that would already have discarded the existing content of the file, but the hypothetical error in the fprintf() would prevent the new content from being written in, and so the original content would be gone but the new content would not be there.
Best Answer