Updated May 19, 2023
Introduction to format long Matlab
The format functions are used in various computer-based languages to get the output in the desired format. In the case of Matlab, the format function is used to set the result 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 the display format given by ‘type’
- If we do not pass any ‘type’, then by default, the format function will set the display format of output to ‘short’ type.
The long type has the following variations:
- long: 15 digits are displayed after the decimal point.
- longEng: 15 digits and an exponential, which is a 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 the 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
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:
As we can see, we have obtained our output in 15 digits because we used ‘long’ as a 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
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. Since we have used longEng as our type, our output will be restricted to 15 digits, 3 before decimal, and 12 after that. Also, we will get an exponential which is a multiple of three]Code:
format longEng
X = 1/3
Output:
As we can see, we have obtained our output with 3 digits before and 12 digits after decimal notation because we used ‘longEng’ as a type for the format function.
Example #3
Let us now see the code to use the format function for longE type in Matlab.
In this example, we will take an array of random numbers and use longE type to format the output.
We will follow the following 2 steps:
- Pass the type as ‘longE’ to the format function.
- Create the input array of random numbers.
Syntax:
format longE
A = rand (3)
[Creating the array of random numbers] [longE will give us the output in scientific notation and with 15 digits after decimal]Code:
format longE
A = rand (3)
Output:
As we can see, we have obtained our output in scientific notation and 15 digits after decimal because we used ‘longE’ as a type for the format function.
Example #4
Let us now see the code to use the format function for longG type in Matlab.
In this example, we will take the same array as used above and will use longG type to format the output.
We will follow the following 2 steps:
- Pass the type as ‘longG’ to the format function.
- Create the input array of random numbers.
Syntax:
format longG
A = rand (3)
[Creating the array of random numbers] [longG will display the output in either scientific notation or in fixed decimal format, depending upon which format is more compact]Code:
format longG
A = rand (3)
Output:
Conclusion
To get the output in the format we want, we can utilise Matlab’s format function. If we anticipate that our output will include around 15 digits, we can utilise a long format.
Recommended Articles
We hope that this EDUCBA information on “format long Matlab” was beneficial to you. You can view EDUCBA’s recommended articles for more information.