输出文本到Octave控制台

问题描述 投票:5回答:2

让我们说我有一个变量A=5,我想输出它,但前面和之后添加了一些文字。像这样的东西:"There are 5 horses."(记住5应该是可变的变量A

如果我写:disp("There are "),disp(A),disp(" horses.")我得到:

There are 
5
 horses.

但我希望一切都在一条线上。

我怎么做?

console line octave output
2个回答
12
投票

您可以使用:

A = 5
printf("There are %d horses\n", A)

输出:

There are 5 horses

甚至

disp(["There are ", num2str(A), " horses"])

甚至

disp(strcat("There are ", num2str(A), " horses"))

但你必须添加一些东西,因为octave / matlab不会让字符串末尾的空格,所以输出是:

ans = There are5 horses

0
投票

根据official documentation

请注意,disp的输出始终以换行符结尾。

所以为了避免换行,你应该使用替代方法为每个字符串输出数据,或者首先连接一个字符串然后disp它。

ThiS listed options

© www.soinside.com 2019 - 2024. All rights reserved.