Introduction to 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
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 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. 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:
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.
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 will use longE type to format the output.
We will follow the following 2 steps:
- Pass the type as ‘longE’ to 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 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 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
format function can be used in Matlab to get the output in the format as desired by us Long format can be used if we expect our output to be of 15 digits or so.
Recommended Articles
This is a guide to format long Matlab. Here we discuss the introduction to format long Matlab along with examples respectively for better understanding. You may also have a look at the following articles to learn more –
3 Online Courses | 1 Hands-on Project | 8+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses