フィルターのクリア

disp vs fpintf?

462 ビュー (過去 30 日間)
ABTJ
ABTJ 2020 年 6 月 13 日
コメント済み: Image Analyst 2020 年 6 月 14 日
What is difference between disp command and fprintf command? when to use former and when to use latter?

採用された回答

Image Analyst
Image Analyst 2020 年 6 月 13 日
disp() just displays the variable value in some default format. fprintf() gives you complete control over the format and how the printed output looks.
  2 件のコメント
ABTJ
ABTJ 2020 年 6 月 14 日
Please explain with example if easy for you. Especially i am confused regarding limitations of both commands. In which applications/scenarios one command should be used while in which applications should we use second command?
Image Analyst
Image Analyst 2020 年 6 月 14 日
Try this:
myValue1 = exp(1);
myValue2 = pi;
disp('Here are the results of disp()');
format short g;
disp(myValue1)
disp(myValue2)
format long g;
disp(myValue1)
disp(myValue2)
fprintf('\nHere are the results for fprintf().\n');
fprintf(' The value for e is %f (with default number of decimal places).\n The value for pi (to 5 decimal places) is %.5f.\n', myValue1, myValue2);
to see, in the command window, the bare bones, basic stuff given by disp(), and the highly customized output that I told fprintf() to make:
Here are the results of disp()
2.7183
3.1416
2.71828182845905
3.14159265358979
Here are the results for fprintf().
The value for e is 2.718282 (with default number of decimal places).
The value for pi (to 5 decimal places) is 3.14159.

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

その他の回答 (1 件)

madhan ravi
madhan ravi 2020 年 6 月 13 日
編集済み: madhan ravi 2020 年 6 月 13 日

カテゴリ

Help Center および File ExchangeScope Variables and Generate Names についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by