Is it possible in matlab to explicitly format the output numbers?

前端 未结 7 1929
太阳男子
太阳男子 2020-12-02 02:07

I know about MATLAB\'s format long, format short, eng ... and so on. But short and long will always display a predefined number of decimals, with

相关标签:
7条回答
  • 2020-12-02 02:43

    I just use sprintf and num2str like mtrw mentioned. sprintf() is meant for multiple scalar arguments of various types (it works w/ matrices, but not really in a clear way). num2str() is meant for use with a single matrix.

    0 讨论(0)
  • 2020-12-02 02:50

    According to the Matlab documentation which is available on their website you can use

    format shortG
    

    to set the display format for the output to n.4.

    0 讨论(0)
  • 2020-12-02 02:55

    Put on top of the Matlab (.m) File the following statement "format shortG".

    Example:

    format shortG;
    
    Q = [0 0.54994 0.1998 0.1998;
     0 0.54994 0.1998 0.1998;
     0 0.54994 0.1998 0.1998;
    ];
    
    disp(Q);
    

    More options are available : Matlab output format

    0 讨论(0)
  • 2020-12-02 03:00

    According to the documentation it does allow you to format the number.

    Also, the formatting is well documented as well.

    0 讨论(0)
  • 2020-12-02 03:03

    There are two simple solutions:

    1. using the sprintf function:

      str = sprintf('%.4f', myNumber);

    2. using Java-based formatting, which is much more powerful (more information):

      str = char(java.text.DecimalFormat('#.0000').format(myNumber));

    0 讨论(0)
  • 2020-12-02 03:06

    I don't know of a way to specify a global format of the type you want. sprintf('%15.4f', x) or num2str(x, '%15.4f') do what you're looking for, if you don't mind calling them explicitly each time.

    0 讨论(0)
提交回复
热议问题