让我们说我有一个变量A=5
,我想输出它,但前面和之后添加了一些文字。像这样的东西:"There are 5 horses."
(记住5
应该是可变的变量A
)
如果我写:disp("There are "),disp(A),disp(" horses.")
我得到:
There are
5
horses.
但我希望一切都在一条线上。
我怎么做?
您可以使用:
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