fprintf output line break

1,058 ビュー (過去 30 日間)
Nikolay Rodionov
Nikolay Rodionov 2012 年 8 月 4 日
コメント済み: LeChat 2021 年 8 月 17 日
Hi all,
I am writing a code where under a string is generated and stored in an array.
After the array is stored, I use fprintf to write the array into an data file, and then later on the string within the array is replaced within a new string and the cycle repeats.
The problem I am having is that I have been unable to make each fprintf cmd to print in a new line in the output file so I get something looking like this:
dataline dataline dataline dataline dataline dataline dataline
...instead of this:
dataline
dataline
dataline
dataline
dataline
I've been tinkering with formatting but I still can't get it right. Can any one help me?
Thanks!!!
  1 件のコメント
Joao Linhares
Joao Linhares 2016 年 11 月 10 日
On my side it worked by:
fprintf(fid, '%s', thestring);
fprintf(fid,'\n');
So basically if you split the code it works better.
Cheers

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2012 年 8 月 4 日
fprintf(fid, '%s\n', TheString);
  3 件のコメント
Midhulesh Vellanki
Midhulesh Vellanki 2017 年 11 月 1 日
This works, but open your file in Notepad++ or other application, default windows application does not represent the correct format, sometime shows new line as a space.
Walter Roberson
Walter Roberson 2019 年 2 月 22 日
On Windows, when you fopen() your file then use 'wt' instead of just 'w' to force CR+NL to be output instead of just NL

サインインしてコメントする。

その他の回答 (4 件)

Image Analyst
Image Analyst 2012 年 8 月 4 日
Try
fprintf(fid, '%s\n\r', TheString);
If that doesn't work try it with the n and r reversed.
fprintf(fid, '%s\r\n', TheString);
One of them should work.
  1 件のコメント
LeChat
LeChat 2021 年 8 月 17 日
Thank you!
fprintf(fid, '%s\r\n', TheString);
worked for me (Matlab 2021a on Windows).

サインインしてコメントする。


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);
  2 件のコメント
Walter Roberson
Walter Roberson 2019 年 2 月 22 日
That would not affect the user's situation. This was a newline vs carriage-return+newline matter.
Matthew Cooper
Matthew Cooper 2019 年 2 月 22 日
Can you suggest a better thread for my contribution? When you invert my answer for a question and ask it, you arrive here. It would have helped me, I am sure it will help someone else. Your clarification is also helpful. Cheers.

サインインしてコメントする。


Samu TanakaBlitch
Samu TanakaBlitch 2016 年 4 月 22 日
I was having the same issue earlier today when i was writing the code and other solutions i found on the internet was not working so i just inserted a disp('"space"') to make it push to the next line.
(earlier it was like this)
(afterwards)
(example of working one)
fprintf( 'The peak resultant head acceleration experienced in the TC-14 crash test is %4.2f g.',peak)
disp(' ')
fprintf('The peak resultant head acceleration experienced in the HCR-16 crash test is %4.2f g.',peakhcr)
disp(' ')
fprintf('The peak resultant head acceleration experienced in the FF-15 crash test is %4.2f g.',peakff)
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 4 月 22 日
You should have just put in \n
fprintf( 'The peak resultant head acceleration experienced in the TC-14 crash test is %4.2f g.\n',peak)

サインインしてコメントする。


Christine Itchon
Christine Itchon 2020 年 6 月 19 日
What do you think would happen if the newline character were omitted from the fprintf statement below?
>> fprintf(‘The value is %d, for sure!\n’,4^3)
  1 件のコメント
Image Analyst
Image Analyst 2020 年 6 月 19 日
Go ahead and try it.
>> fprintf('The value is %d, for sure!\n',4^3)
The value is 64, for sure!
>> fprintf('The value is %d, for sure!',4^3)
The value is 64, for sure!>>
As you can see if the final newline is omitted, the >> prompt does not happen on the next line. It happens on the same line.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by