Question answered, thanks: question on line break using fprintf

Hi
I need to write several lines of text to the user. I am using fprintf, but even though I write the text on consecutive lines, using fprintf each time, it all gets put together to one long string when I execute the program. Could someone please tell me how I get line breaks in the text?
Thanks a bunch!

 채택된 답변

Star Strider
Star Strider 2015년 3월 31일
편집: Star Strider 2015년 3월 31일
Include the ‘newline’ ('\n') character at the end of each line:
fprintf('This is the first line\n')
fprintf('This is the second line with two newline characters to provide a blank line\n\n')
fprintf('This is the third line\n')
See the documentation for fprintf, specifically Text Before or After Formatting Operators, for details.

추가 답변 (1개)

James Tursa
James Tursa 2015년 3월 31일
Include the newline \n at the end of the text where you want the line break. E.g.,
fprintf("This is the first line\n");
fprintf("This is the second line\n");

댓글 수: 3

Hi James Thanks for your quick answer. I tried it and unfortunately, if didn't work. It just printed the /n in the string and then continued with the next string. Any ideas?
Works now, James, thanks a bunch!
Karen, note that /n is different than \n. The backslash is a special character that means the next character has a special meaning, while the forward slash is just a regular character. That's why you saw what you saw.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

다시 열림:

2018년 6월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by