How can i represent a number let's say 1231312.1232323 with the formats long/short/long e /short e /rat more elegant or in a single code line without having to write 10 colums i was thinking of the fprintf and format spec commands . P.S i'm very much a beginner in matlab 🙂 .
MATLAB: Format short, format long, format short E, format long E, format rat
formatMATLABrepresentation formats
Related Solutions
The FORMAT function allows users to set the default display method for floating point numbers in MATLAB.
The options for format are:
FORMAT Default. Same as SHORT. FORMAT SHORT Scaled fixed point format with 5 digits. FORMAT LONG Scaled fixed point format with 15 digits. FORMAT SHORT E Floating point format with 5 digits. FORMAT LONG E Floating point format with 15 digits. FORMAT SHORT G Best of fixed or floating point format with 5 digits. FORMAT LONG G Best of fixed or floating point format with 15 digits. FORMAT HEX Hexadecimal format. FORMAT + The symbols +, - and blank are printed for positive, negative and zero elements. Imaginary parts are ignored. FORMAT BANK Fixed format for dollars and cents. FORMAT RAT Approximation by ratio of small integers. Spacing: FORMAT COMPACT Suppress extra line-feeds. FORMAT LOOSE Puts the extra line-feeds back in.
For more information you should see the FORMAT documentation, accessible from MATLAB with the following command:
doc format
Here is a quick example:
>> format short>> pians = 3.1416>> format long>> pians = 3.14159265358979
Currently, it is not possible to specify your own precision in MATLAB. A suggested work-around to this issue would be to use the CEIL and FLOOR functions, in combination with multiplication by magnitudes of 10, to round off to a particular precision.
For example, if you have a variable x = 3.1416, to obtain only three significant digits, you should do the following:
format shortx=pixtemp=x*1000xtempt=ceil(xtemp)x=xtempt/1000
If you have one variable that is single precision and one that is double precision, you can perform your calculation with them as you normally would. The result will be single precision.
- You can perform basic arithmetic operations with single and any of the following other classes. The result is always single:
single double char logical
Best Answer