Categories
2. After the Advance

format long Matlab

The format functions are used in various computer based languages to get the output in the desired format. In case of Matlab the format function is used to set the output obtained in the command window to the desired format. Format function has various types or styles like short, long, hex etc.

Syntax:

format type

format

Description:

  • format type is used to change the display format of output in Matlab’s Command Window and set it to display format given by ‘type’
  • If we do not pass any ‘type’, then by default, format function will set the display format of output to ‘short’ type.

The long type has following variations:

  • long: 15 digits are displayed after the decimal point.
  • longEng: 15 digits and an exponential which is multiple of three.
  • longE: 15 digits are displayed in scientific notation.
  • longG: It displays the output in either scientific notation or in fixed decimal format, depending upon which format is more compact.

Examples of format long Matlab

Given below are the examples mentioned:

Example 1:

In this example, we will take division of 2 integers and will use the format function to get the output in long format.

We will follow the following 2 steps:

  • Use format function with type ‘long’.
  • Perform division of 2 integers.

Syntax:

format long[Passing the type as ‘long’ to the format function]

X = 1/3[Initializing a variable and passing division of 2 integers into it] [Mathematically, the value of1/3 is 0.3333….. which goes on till infinity. Now since we have used long as our type, our output will be restricted to 15 digits]

Code:

format long
X = 1/3

Output:

format long Matlab 1

As we can see, we have obtained our output in 15 digits because we used ‘long’ as type for the format function.

Example 2:

Let us now see the code to use the format function for longEng type in Matlab.

In this example, we will take the same example as we used above for better understanding.

We will follow the following 2 steps:

  • Use format function with type longEng.
  • Perform division of 2 integers.

Syntax:

format longEng[Passing the type as ‘longEng’ to the format function]

X = 1/3[Initializing a variable and passing division of 2 integers into it] [Mathematically, the 1/3 is 0.3333….. which goes on till infinity. Now since we have used longEng as our type, our output will be restricted to 15 digits, 3 before decimal, 12 after that. Also, we will get an exponential which is multiple of three]

Code:

format longEng
X = 1/3

Output:

format long Matlab 2

As we can see, we have obtained our output with 3 digits before and 12 digits after decimal notation because we used ‘longEng’ as type for the format function.

Leave a Reply

Your email address will not be published. Required fields are marked *