26

x" " とという変数があるとしx=5ます。

私はやりたい:

disp('x is equal to ' + x +'.');

そのコードを印刷します:

x は 5 です。

これは、私が Java で行うのに慣れている方法であるため、MATLAB でこれを行うのと同様の方法である必要があります。

ありがとう

4

3 に答える 3

69

If you want to use disp, you can construct the string to display like so:

disp(['x is equal to ',num2str(x),'.'])

I personally prefer to use fprintf, which would use the following syntax (and gives me some control over formatting of the value of x)

fprintf('x is equal to %6.2f.\n',x);

You can, of course, also supply x as string, and get the same output as disp (give or take a few line breaks).

fprintf('x is equal to %s\n',num2str(x))
于 2011-10-05T04:50:58.923 に答える
1

matlab でいくつかのスカラー変数を出力するのは面倒です (上記の回答を参照)。検索パスに次のような機能があると役立ちます。

function echo(varargin)
str = '';
for k=1:length(varargin)
    str = [str ' ' num2str(varargin{k})];
end 
disp(str)
于 2015-02-28T18:04:39.950 に答える
0

disp() 内に sprintf() をネストするだけです。

    disp(sprintf("X is equal to %d.",x));
于 2019-08-13T20:30:41.270 に答える