How do I use a user input for a desired number of decimal places in an fprintf or similar statement? The instructions are to use the user's input to display or print the correct number of decimal places once calculations are performed. Thanks!

4 ビュー (過去 30 日間)
I have tried using something like this to no avail:
fprintf('# Negative:\t\t\t\t\t%0.%f',decimal,neg)
Perhaps I need an sprintf statement or disp after the '# Negative' string? Any suggestions are most appreciated.
  4 件のコメント
Stephen23
Stephen23 2017 年 3 月 5 日
According to the fprintf documentation: "The precision operator can be a number, or an asterisk (*) to refer to an argument."
"Example: The input arguments ('%6.4f', pi) are equivalent to ('%*.*f', 6, 4, pi)."
It works, try it:
>> fprintf('%.*f\n', 5, pi)
3.14159
Jenel Darland
Jenel Darland 2017 年 3 月 6 日
Ahh, I missed that the first time around. Thanks very much for your help!

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

採用された回答

Walter Roberson
Walter Roberson 2017 年 3 月 5 日
fprintf('whatever %.*f', decimal, neg)
  1 件のコメント
Jenel Darland
Jenel Darland 2017 年 3 月 6 日
Thank you SO much! Sitting there for hours experimenting can get frustrating. Thanks again.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by