how to display newline

조회 수: 480 (최근 30일)
Tor Fredrik Hove
Tor Fredrik Hove 2011년 10월 16일
댓글: Walter Roberson 2024년 11월 17일
Can you use newline seperately or do you always have to use it in fprintf in order to make it work. Theese are my attempts with newline:
this did not work:
This gave a strange result
Is this the only way to make a newline?:
Here is my script for the last line:
rows=3;
columns=5;
for i=1:rows
for j=1:columns
fprintf('*')
end
fprintf('\n')
end
  댓글 수: 1
the cyclist
the cyclist 2011년 10월 16일
I think you got the answer you need from Wayne, but for future reference, you posted three copies of the same image in this question.

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

채택된 답변

Wayne King
Wayne King 2011년 10월 16일
Hi, '\n' by itself outside of special calls is just interpreted as the string (character array) \n
It's not just in fprintf(), sprintf() also interprets escape characters correctly like \n or \t
but yes you're right:
disp('Hi \n nice to meet you')
% produces Hi \n nice to meet you
but
fprintf('Hi \n nice to meet you')
% Hi
% nice to meet you>>
  댓글 수: 4
Walter Roberson
Walter Roberson 2020년 9월 19일
disp(sprintf('Hi \n nice to meet you'))
Sina Davari
Sina Davari 2020년 12월 11일
Hi;
You could simply put { disp(' '); } between two disp for obtaining an empty line.
disp('Hi'); disp(' '); disp('nice to meet you')

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

추가 답변 (3개)

Steven Lord
Steven Lord 2022년 9월 6일
If you're using release R2016b or later you can use the newline function, perhaps in conjunction with a string array.
s = "apple" + newline + "banana"
s =
"apple banana"
c = ['apple', newline, 'banana']
c =
'apple banana'
  댓글 수: 2
Vishal
Vishal 2024년 11월 14일
If you want to use newline in verisons below 2016b you can use following comment.
string2Print=sprintf('%s\n','Hi','nice to meet you')
Walter Roberson
Walter Roberson 2024년 11월 14일
That is what @Saul Stokar suggested two years ago.

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


Kleber Zuza Nobrega
Kleber Zuza Nobrega 2020년 9월 19일
supose you want to disp the word 'Ball'. Try
a=['Ball',char(13),'to a new line']
disp(a)
  댓글 수: 4
Walter Roberson
Walter Roberson 2020년 11월 15일
Or
disp("Ball" + newline + "to a new line");
Daniel Gonzalez
Daniel Gonzalez 2021년 8월 25일
char(10) worked for me, but great advice.

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


Saul Stokar
Saul Stokar 2022년 9월 6일
Youj can do it using sprintf :
string2Print=sprintf('%s\n','Hi','nice to meet you');
disp(string2Print)
This prints:
Hi
nice to meet you
  댓글 수: 2
Saul Stokar
Saul Stokar 2024년 11월 17일
Note that you can avoid the extra newline after the last printout as well:
disp(sprintf('%s\n%s\n%s','first line','second line','third line')) prints
first line
second line
third line
(without a newline after the last line printed)
while disp(sprintf('%s\n','first line','second line','third line')) prints
first line
second line
third line
(with a newline after the last line printed)
Walter Roberson
Walter Roberson 2024년 11월 17일
... unless you are using LiveScript or MATLAB Online or MATLAB Answers. All three of those automatically add the "missing" newline after the last output display on each logical line of code (logical line is physical line except taking into account ... line continuation).

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

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by