【问题标题】:Print in Command Window without 'ans = ' in matlab?在matlab中没有'ans ='的命令行窗口打印?
【发布时间】: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


    【解决方案1】:

    您可以使用fprintf 代替sprintf。请记住在字符串末尾添加换行符\n

    【讨论】:

    • 哦...很简单,谢谢(需要等待才能接受)。
    • @jucestain 感谢您的信息
    【解决方案2】:

    总结

    选项 1disp(['A string: ' s ' and a number: ' num2str(x)])

    选项 2disp(sprintf('A string: %s and a number %d', s, x))

    选项 3fprintf('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.

    【讨论】:

      猜你喜欢
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多