12

変数がA=5あり、それを出力したいのですが、その前後にテキストが追加されているとしましょう。このようなもの :(変更可能な変数でなければならないことに注意"There are 5 horses." してください)5A

私が書くと:disp("There are "),disp(A),disp(" horses.") 私は得る:

There are 
5
 horses.

しかし、すべてを1行にまとめたいです。

それ、どうやったら出来るの?

4

2 に答える 2

21

次を使用できます。

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
于 2013-03-07T09:40:43.117 に答える