When I use sprintf, the results show like this :
sprintf('number=%d %d %d',a,b,c)
sprintf('or %d',h)
ans =
number= 5 4 2
ans =
or 2
How can I display the results without ans =
obstructing them ?
When I use sprintf, the results show like this :
sprintf('number=%d %d %d',a,b,c)
sprintf('or %d',h)
ans =
number= 5 4 2
ans =
or 2
How can I display the results without ans =
obstructing them ?
You can use fprintf
instead of sprintf
. Remember to put a newline \n
at the end of your strings.
Option 1: disp(['A string: ' s ' and a number: ' num2str(x)])
Option 2: disp(sprintf('A string: %s and a number %d', s, x))
Option 3: fprintf('A string: %s and a number %d\n', s, x)
Quoting http://www.mathworks.com/help/matlab/ref/disp.html (Display Multiple Variables on Same Line)
There are three ways to display multiple variables on the same line in the Command Window.
(1) Concatenate multiple strings together using the [] operator. Convert any numeric values to characters using the num2str function. Then, use disp to display the string.
name = 'Alice';
age = 12;
X = [name,' will be ',num2str(age),' this year.'];
disp(X)
Alice will be 12 this year.
(2) You also can use sprintf to create a string. Terminate the sprintf command with a semicolon to prevent "X = " from being displayed. Then, use disp to display the string.
name = 'Alice';
age = 12;
X = sprintf('%s will be %d this year.',name,age);
disp(X)
Alice will be 12 this year.
(3) Alternatively, use fprintf to create and display the string. Unlike the sprintf function, fprintf does not display the "X = " text. However, you need to end the string with the newline (\n) metacharacter to terminate its display properly.
name = 'Alice';
age = 12;
X = fprintf('%s will be %d this year.\n',name,age);
Alice will be 12 this year.
© 2022 - 2024 — McMap. All rights reserved.