how to display newline
1,154 次查看(过去 30 天)
显示 更早的评论
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
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
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 个评论
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
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"
c = ['apple', newline, 'banana']
2 个评论
Vishal
2024-11-14,10:05
If you want to use newline in verisons below 2016b you can use following comment.
string2Print=sprintf('%s\n','Hi','nice to meet you')
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 个评论
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
2024-11-17,9:10
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
2024-11-17,17:22
... 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 Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!