【发布时间】:2013-03-21 20:22:40
【问题描述】:
当我使用 sprintf 时,结果显示如下:
sprintf('number=%d %d %d',a,b,c)
sprintf('or %d',h)
ans =
number= 5 4 2
ans =
or 2
如何在没有ans = 阻碍的情况下显示结果?
【问题讨论】:
标签: matlab printf command-window
当我使用 sprintf 时,结果显示如下:
sprintf('number=%d %d %d',a,b,c)
sprintf('or %d',h)
ans =
number= 5 4 2
ans =
or 2
如何在没有ans = 阻碍的情况下显示结果?
【问题讨论】:
标签: matlab printf command-window
您可以使用fprintf 代替sprintf。请记住在字符串末尾添加换行符\n。
【讨论】:
选项 1:disp(['A string: ' s ' and a number: ' num2str(x)])
选项 2:disp(sprintf('A string: %s and a number %d', s, x))
选项 3:fprintf('A string: %s and a number %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 创建一个字符串。使用分号终止 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 will be 12 this year.
【讨论】: