how to display newline

508 visualizzazioni (ultimi 30 giorni)
Tor Fredrik Hove
Tor Fredrik Hove il 16 Ott 2011
Commentato: DGM il 29 Ott 2025 alle 0:32
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 Commento
the cyclist
the cyclist il 16 Ott 2011
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.

Accedi per commentare.

Risposta accettata

Wayne King
Wayne King il 16 Ott 2011
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 Commenti
Walter Roberson
Walter Roberson il 19 Set 2020
disp(sprintf('Hi \n nice to meet you'))
Sina Davari
Sina Davari il 11 Dic 2020
Hi;
You could simply put { disp(' '); } between two disp for obtaining an empty line.
disp('Hi'); disp(' '); disp('nice to meet you')

Accedi per commentare.

Più risposte (4)

Steven Lord
Steven Lord il 6 Set 2022
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 Commenti
Vishal
Vishal il 14 Nov 2024
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 il 14 Nov 2024
That is what @Saul Stokar suggested two years ago.

Accedi per commentare.


Kleber Zuza Nobrega
Kleber Zuza Nobrega il 19 Set 2020
supose you want to disp the word 'Ball'. Try
a=['Ball',char(13),'to a new line']
disp(a)
  4 Commenti
Walter Roberson
Walter Roberson il 15 Nov 2020
Or
disp("Ball" + newline + "to a new line");
Daniel Gonzalez
Daniel Gonzalez il 25 Ago 2021
char(10) worked for me, but great advice.

Accedi per commentare.


Saul Stokar
Saul Stokar il 6 Set 2022
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 Commenti
Saul Stokar
Saul Stokar il 17 Nov 2024
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 il 17 Nov 2024
... 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).

Accedi per commentare.


Sudesh
Sudesh il 28 Ott 2025 alle 9:26
1️⃣ Using \n in fprintf
Your script works perfectly because fprintf interprets \n as a newline:
fprintf('*'); % prints *
fprintf(
'\n'); % moves to next line
✅ This is the standard way and works reliably.2️⃣ Using newline by itself
newline is actually a string in MATLAB, not a command. For example:
s = newline;
disp(['Hello' s 'World'])
This works because you’re concatenating strings.
However, if you try this:
newline
MATLAB will just return the string representing the newline—it won’t actually move the cursor in the Command Window like fprintf('\n') does.
3️⃣ Using newline with fprintf
You can use it like this:
fprintf(['*\n']); % using \n
fprintf([
'*' newline]); % using newline
Both work, but fprintf(newline) alone will not, because fprintf expects a string with content to print. So you must provide it either inside a string or concatenated with text.
  2 Commenti
Stephen23
Stephen23 il 28 Ott 2025 alle 9:52
Modificato: Stephen23 il 28 Ott 2025 alle 9:55
"newline is actually a string in MATLAB, not a command"
NEWLINE is actually a built-in function. Lets check this using three different approaches:
1) WHICH tells us directly if a function is built-in:
which newline -all
built-in (/MATLAB/toolbox/matlab/strfun/newline)
2) EXIST returns a value of 5 for inbuilt functions:
exist newline
ans = 5
3) FUNCTIONS will (since R2020-ish) tell us if the handle refers to a built-in function:
fnh = @newline
fnh = function_handle with value:
@newline
functions(fnh)
ans = struct with fields:
function: 'newline' type: 'simple' file: 'MATLAB built-in function'
DGM
DGM il 29 Ott 2025 alle 0:32
"newline is actually a string in MATLAB, not a command"
If we want to be picky, the newline() function was not available in versions prior to R2016b, but neither was the string class. If it's not a function, then it's also not a string. In fact, it's a character. That's what we're using here.
Perhaps the distinction that would have made more sense is that the purpose of newline() is not to directly increment the console, but to output a variable containing char(10). How this output is displayed when you echo the assignment to console is exactly the same as any other variable of its class.
% assign the char vector 'banana' to a variable
a = 'banana'
a = 'banana'
% assign char(10) to a variable
% this behavior is no different than the prior line
b = newline()
b =
' '
If you have text (e.g. the output of newline()) and you want to actually print it to console without the variable name and quotes, then you either have to use disp() or fprintf().
fprintf(a)
banana
fprintf(b)
"Both work, but fprintf(newline) alone will not, because fprintf expects a string with content to print."
This is confusing and I don't see the point. It's not that fprintf() expects non-whitespace text inputs. You're simply instructing it to print nothing but the newline. If you're expecting text followed by a newline, then this isn't the fault of fprintf(). It printed exactly what you told it to print. I'm going to modify this so that the newline is more obvious:
fprintf(['*\n*']); % print two asterisks with a newline between
* *
fprintf(['*' newline '*']); % print two asterisks with a newline between
* *
fprintf(newline); % only print the newline and nothing else
"So you must provide it either inside a string or concatenated with text."
Again, this is confusing. If we're working with the informal usage of the word "string", then "inside a string" and "concatenated with text" are the same thing. The two examples you gave are exactly that. Again, I've modified this to make the newline obvious:
% the input to fprintf() is char([42 10 42])
% the outer concatenation operator does nothing here
fprintf(['*\n*']);
* *
% the input to fprintf() is [char(42),char(10),char(42)]
% which is identical to the input above
fprintf(['*' newline '*']);
* *
Alternatively, instead of trying to construct the format expression from a bunch of inputs by direct concatenation, you can use the appropriate format operators and include those variables in the input argument list.
fruit = 'apple';
pastry = 'pie';
fprintf('%s\n%s',fruit,pastry);
apple pie

Accedi per commentare.

Categorie

Scopri di più su Entering Commands in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by