Print in Command Window without 'ans = ' in matlab?
Asked Answered
W

2

7

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 ?

Wycliffite answered 21/3, 2013 at 20:22 Comment(0)
G
6

You can use fprintf instead of sprintf. Remember to put a newline \n at the end of your strings.

Goerke answered 21/3, 2013 at 20:26 Comment(1)
Oh ... that was simple, thank you (need to wait before accepting).Wycliffite
P
6

Summary

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)

Details

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.

Papageno answered 29/11, 2014 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.