# MATLAB: Edit line in text document

fopenfprintftext file

Is there a way to change one line in a text document? My impression is that with fopen and fprintf there is no way to just edit the contents a line, leaving the rest of the doc unchanged. I tried the following:
fid = fopen(doc,'r+);while true  str = fgetl(fid);   if feof(fid) % break out of loop and end of doc      break   end   if strcmp(str,checkstr)      newstr = [str 'abc'];      fprintf(fid,'%s',newstr);   endendfclose(fid);
But I cannot get this to work (if I run the code, the document is not changed). I tried to play with additional tags like \r or \n, but I couldn't get it to work. Is there something I am missing, or is it generally not possible (with reasonable effort) to just edit a text file, in which case I guess I will have to create a copy of the full file?

 buffer = fileread('inFile.txt') ; buffer = regexprep(buffer, 'I hate regexp', 'I love regexp') ; fid = fopen('outFile.txt', 'w') ; fwrite(fid, buffer) ; fclose(fid) ;