how to print multiple variables with strings in between ?

51 ビュー (過去 30 日間)
Ahmed Al-Qarqaz
Ahmed Al-Qarqaz 2020 年 3 月 31 日
コメント済み: James Tursa 2020 年 3 月 31 日
im trying to write a code for a function that prints (outputs) a couple of variables .
basically what im trying to do is similar to this c++ code:
cout << " variable a equals: " << a << "varaible b equals: "<< b;
is there a way to do this in matlab ?

採用された回答

James Tursa
James Tursa 2020 年 3 月 31 日
編集済み: James Tursa 2020 年 3 月 31 日
You could use fprintf. E.g.
>> a = 5;
>> b = 7;
>> fprintf("Variable a equals: %g\n",a);
Variable a equals: 5
>> fprintf("Variable b equals: %g\n",b);
Variable b equals: 7
>> fprintf("Variable %s equals: %g\n",'a',a);
Variable a equals: 5
>> fprintf("Variable %s equals: %g\n",'b',b);
Variable b equals: 7
If you really want the text strung out in one line, omit the \n part.
  2 件のコメント
Ahmed Al-Qarqaz
Ahmed Al-Qarqaz 2020 年 3 月 31 日
what does %g mean ?
James Tursa
James Tursa 2020 年 3 月 31 日
%g is a generic format adjusting to size of value. There are others available. E.g., you could use %f for a fixed format, etc.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by