在Matlab中如何在命令窗口打印时不显示“ans =”?

7
当我使用sprintf时,结果如下所示:
sprintf('number=%d %d %d',a,b,c)
sprintf('or %d',h)  

ans = 

number= 5 4 2

ans =

or 2

如何在不妨碍结果的情况下显示它们,而无需使用 ans =
2个回答

6

摘要

选项1: disp(['字符串: ' s ' 和数字: ' num2str(x)])

选项2: disp(sprintf('字符串: %s 和数字: %d', s, x))

选项3: fprintf('字符串: %s 和数字: %d\n', s, x)

详情

引用http://www.mathworks.com/help/matlab/ref/disp.html(在同一行上显示多个变量)

有三种方法可以在命令窗口中同时显示多个变量。

(1) 使用 [] 操作符将多个字符串连接在一起。使用 num2str 函数将任何数字值转换为字符。然后使用 disp 显示字符串。

name = 'Alice';   
age = 12;
X = [name,' will be ',num2str(age),' this year.'];
disp(X)

Alice will be 12 this year.

(2) 您还可以使用sprintf创建一个字符串。以分号结尾以防止“X =”被显示。然后使用disp来显示字符串。

name = 'Alice';   
age = 12;
X = sprintf('%s will be %d this year.',name,age);
disp(X)

Alice will be 12 this year.

(3) 或者使用fprintf创建和显示字符串。与sprintf函数不同的是,fprintf不会显示“X=”文本。但是,您需要以换行符(\n)元字符结尾字符串,以正确终止其显示。

name = 'Alice';   
age = 12;
X = fprintf('%s will be %d this year.\n',name,age);

Alice今年将满12岁。


6
你可以使用fprintf代替sprintf。记得在字符串末尾添加换行符\n

哦...那很简单,谢谢(需要等待才能接受)。 - NLed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接