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

时光毁灭记忆、已成空白 提交于 2019-12-17 10:01:40

问题


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 an exponent, and for example, format bank will display always 2 decimals.

Is there an option to put your explicit format, in a "fortran way", like f8.3 --> 1234.678 ?

I'm looking for a way to display numbers with 4 decimal points, and the rest ahead of the decimal point, with no exponent.


回答1:


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.




回答2:


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.




回答3:


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

Also, the formatting is well documented as well.




回答4:


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




回答5:


The closest I could come up with, is:

format bank

It will give you no E, and 2 decimal places.

try to read

help format

to look for other options there (I don't use Matlab anymore... switched to Free Software :) )




回答6:


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.




回答7:


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



来源:https://stackoverflow.com/questions/1759982/is-it-possible-in-matlab-to-explicitly-format-the-output-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!