Is it possible in Matlab to explicitly format output numbers?

I know the MATLAB long format, the short format, eng ... and so on. But short and long will always display a predetermined number of decimal places with an exponent, and, for example, the bank format will always display two decimal places.

Is there any way to put your explicit format in "fortran" mode, for example, f8.3 β†’ 1234.678?

I am looking for a way to display numbers with four decimal points, and the rest before the decimal point without an exponent.

+4
source share
7 answers

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

+11
source

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

 format shortG 

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

+6
source

In accordance with the documentation, it allows you to format the number.

Also formatting is well documented.

+5
source

Closest I could come up with:

 format bank 

This will not give you E and 2 decimal places.

try to read

 help format 

look for other options there (I no longer use Matlab ... switched to free software :))

+2
source

There are two simple solutions:

  • using the sprintf function:

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

  • using Java-based formatting, which is much more powerful ( more info ):

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

+2
source

I just use sprintf and num2str as mentioned by mtrw. sprintf() designed for many scalar arguments of different types (it works with matrices, but not quite clearly). num2str() intended for use with a single matrix.

0
source

Place Matlab (.m) on top Write the following "format shortG" statement.

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); 

Additional options are available: Matlab output format

0
source

All Articles