fprintf doesn't create new lines
86 views (last 30 days)
Show older comments
Output txt file is a single row of text, ignoring the fprintf new line command. I have checked the file in the matlab editor, notepad, and several different other text editors, and all show the same.
I have tried \n, \r, \r\n, \n\r. None produces a new line. I have also set the fopen as 'wt' and 'at' (following similar questions here), none of them work.
Have tried it in mac and windows, same results
Is there an actual solution for this issue?
Currently working on mac (capitan) on matlab 2015a; but I have the same issue in windows and matlab 2014
9 Comments
Accepted Answer
Thorsten
on 2 Aug 2016
You can fprint to fid == 1 to view the result on the console for testing. This creates newlines
fprintf(1,'%d\n', [1 2 3])
More Answers (2)
Matthew Cooper
on 21 Feb 2019
If you are writing to a file recursively, you may need to substitute 'a' for 'w' on fopen:
fopen(fid,'foo.txt','w');
fprintf(fid,formatspec,text);
fclose(fid);
fopen(fid,'foo.txt','a');
fprintf(fid,formatspec,newtext);
3 Comments
Walter Roberson
on 22 Feb 2019
When 'r' is used, any existing file content is not removed during fopen(). Requests to write content are ignored (no error message.)
When 'w' is used, any existing file has its contents completely removed during the fopen(). The file does not have to already exist. Any new content written, would be written at the "current position", which would start out being at the end of the emptied file (which is the same as the beginning of the emptied file, since the file is empty), but could be changed with fseek(). If the writing that produces the furthest writing into the file does not end with \n then MATLAB will not automatically add a newline at the end: the file will just suddenly end. The sudden ending is not a problem for the file systems, because on all file systems that MATLAB has been implemented on for rather some time, the file system automatically keeps track of the length of the data written.
When 'a' is used, any existing file content is not removed during the fopen(). Any new content written, would be written at the end of file, even if fseek() has been done. If the writting into the file does not end with \n then MATLAB will not automatically add a newline at the end: the file will just suddenly end. The sudden ending is not a problem for the file systems, because on all file systems that MATLAB has been implemented on for rather some time, the file system automatically keeps track of the length of the data written.
When 'r+' is used, any existing file content is not removed during fopen(). Any new content written, would be written at the "current position", which would start out being at the beginning of the file, but could be changed with fseek(). If the writing that produces the furthest writing into the file does not end with \n then MATLAB will not automatically add a newline at the end: the file will just suddenly end. The sudden ending is not a problem for the file systems, because on all file systems that MATLAB has been implemented on for rather some time, the file system automatically keeps track of the length of the data written.
When 'w+' is used, the effect for writing is the same as if 'w' had been used. However, using 'w+' may have effects on the ability to read from the file.
When 'a+' is used, the effect for writing is the same as if 'a' had been used. However, using 'a+' may have effects on the ability to read from the file.
The effects that any of these have on newlines is "NONE".
It is not uncommon for people to assume that if they write something to a file, leaving off a final \n, and then close the file and open it in 'a' mode, that anything new they add will be on a new line after what was written before -- an assumption that the file automatically ends with a newline because nearly all editors show the final line just like the other lines, as if it ended in newline like the other lines do. However this is a mistaken notion: no final \n is automatically added, and any appending to the file will pick up right where the file left off, adding to the end of that line.
Also, it is common for people to want to use dlmwrite() with -append mode to add more data on the end of the same line. That does not work, because dlmwrite() happens to always end the output with \n, and dlmwrite()'s append mode does not "back up" to before the newline: it appends after all existing content in the file, which will be after the \n if you happened to have used dlmwrite() to write the content.
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!