fprintf doesn't create new lines

120 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2016-8-2
编辑:Stephen23 2016-8-2
"Lets say text == "A""
There is no way that you could get a succession of A's using text='A' and your format string:
>> fprintf(1,'%d\n','A')
65
This does however correctly print the character value of 'A' and one newline, as it should. I don't see why you would expect it to produce multiple A's in a column though.
"each line is composed, like I said, of several variables (numeric or strings), separated by tabs"
Which "variables"? You only showed us one: the badly named text. You should never call any variable text, because this is the name of a very important inbuilt function text.
Please show us a complete Minimum Working Example. Please. Three times asking for the same thing is my limit.
Rena Berman
Rena Berman 2016-10-30
(Answers dev) restored question

请先登录,再进行评论。

采纳的回答

Thorsten
Thorsten 2016-8-2
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])
  1 个评论
D.
D. 2016-8-2
Thanks for the fid ==1 tip! that is indeed useful for testing.

请先登录,再进行评论。

更多回答(2 个)

Matthew Cooper
Matthew Cooper 2019-2-21
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 个评论
Matthew Cooper
Matthew Cooper 2019-2-22
Not everyone asks perfect questions. When I searched for an answer to why I could not make \n work, I found this thread. My answer provided the solution. I don't think my answer is doing any harm, but thank you for taking the time to comment.
Walter Roberson
Walter Roberson 2019-2-22
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.

请先登录,再进行评论。


D.
D. 2016-8-2
Copy / pasted script to an external text editor, and then copy/pasted it back from there to matlab editor (To be sure, no editing at all was done between copy/paste operations). Now the output file looks as expected. FYI.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by