I am writing several lines to a text file, among which is a matrix. I decided to use fprintf for the normal text messages and to use dlmwrite for writing the matrix to the file. However, this action is done in a while loop. Here is the outline:
k=0;
while (k<10):
fprintf(file,'%s', 'Hello');
dlmwrite(file,M ,'-append', 'newline', 'pc');
fprintf(file, '%s' , 'Goodbye');
k= K+1;
however, when I open the file, all the matrices are appended to the very end of the text file instead of each being between a hello and a goodbye. Is there a way to fix this issue?
It may have something to do with the
-appendoption, that according to the help appends the result to the end of the file.You are accessing the same file with two functions,
fprintfanddlmwrite.This is probably not efficient, but closing the file after every write from
fprintfwould work:If not, just try to print the matrix with other funcion that you create and that uses the same file handler as
fprintf.