lettura simple

Matlab's Long and Short Numeric Formats

By default, Matlab displays the result of a mathematical operation in integer or decimal format, depending on whether the result is an integer or decimal.

Note. Unlike other programming languages, it does not consider the starting format of the operands. For example, if the operands are decimal numbers, the result may not necessarily be a decimal number as well.

For instance, if you type the sum of two decimal numbers 1.7+3.3, Matlab returns the integer value 5 instead of the decimal value 5.0

>> 1.7+3.3
ans = 5

However, if you type the sum of decimal values 1.7+3.4, Matlab responds with 5.1 because the result is a decimal value.

>> 1.7+3.4
ans = 5.1000

Long and Short Formats

By default, Matlab displays output results in short decimal format, which has five significant figures.

>> 2/3
ans = 0.6667

To display the result with more significant figures, you need to set the long mode by typing the command format long

>> format long
>> 2/3
ans = 0.666666666666667

The long mode remains active until the end of your work session.

You can still switch back to short mode at any time during the current session by typing format short.

Scientific Notation

You can also use the base ten scientific notation to display results in Matlab.

Type format short e or format long e

>> format short e
>> 2/3
ans = 6.6667e-01

When you're finished using scientific notation, type "format short" or "format long" to return to normal display.

Note. If you want to go back to Matlab's default format, simply type "format". The short option is the default.

Alternatively, you can use the commands format short g and format long g.

These two commands enable scientific notation only when necessary. Depending on the result, Matlab chooses whether to use it or not.

>> format short g
>> 2/3
ans = 0.66667
>> 2*100000
ans = 200000

Finally, to display results in exponential format, type format short eng or format short long.

This way, Matlab displays the result in exponential format with an exponent divisible by three.

>> format short eng
>> 2/3
ans = 666.6667e-003
>> 2*100000
ans = 200.0000e+003

Rational Approximation

Rational approximation is a Matlab format that allows you to display real numbers approximated as a fraction.

To activate rational approximation, type the command format rat

For example, if you write 1.2, Matlab automatically converts the real number into the fraction 6/5.

>> format rat
>> 1.2
ans = 6/5




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin