Categories
3. Matlab Functions

Matlab Format

Matlab format function is used for changes in the appearance of output display format. Matlab format function affects only how numbers are displayed in output windows. Matlab format style is used in changes the output display format in the Command Window to the format which is specified by style. Matlab format function by default resets the output format to short, which is used for fixed-decimal format for floating-point notation and loose line spacing for all output lines. In Numeric formats, Matlab format function affect only how numbers appear in Command Window or output Window.

Syntax:

Syntax for Matlab format function is as follows

  • format style
  • format

How does Format Function work in Matlab?

Matlab format function affects only how numbers are displayed in output windows. The format function changes the numbers appear in Command Window or output window to the format which is specified by style. There are different styles like short, Long, short e, long e, short g, long g, short eng. long eng, etc. There is different line spacing for output lines like compact and loose.

Steps for how to format function works is as follows:-

  • Step 1: Take data into a variable or array.
  • Step 2: After that choose the appropriate style using Matlab format function with proper syntax.
  • Step 3: After that choose appropriate line spacing for output lines by using Matlab format function with proper syntax.
  • Step 4: Execute the Matlab code to get output.

Following are the examples are given below:

Example 1:

In this example, we see that how to set the different output formats for pi in Matlab and display the value of pi. In matlab to set output format, we used the Matlab Format function. In this example, we see different format styles and different output formats of pi. Here we used short which is used for Scaled fixed point format, with 5 digits. After that values of pi is displayed. Then we used format style long with the loose format. Style long contain scaled fixed point format, with 15 digits for double after that execute the matlab code to display the value of pi at command widows.

Code:

clc;
close all;
clear all;
format short
format compact
pi
format long
format loose
pi

Output – Command Window

Matlab Format-1.1

After executing the matlab code we get the different output format for values of pi in command windows.

Example 2:

In this example we see how to set the short engineering format output format with a compact line spacing format. For that, we first set the short engineering format with a compact line spacing format using matlab format function with proper syntax. “format shortEng and format compact” this line set the short engineering format with a compact line spacing format and then we take random values in variable x. By default, the format is short which is scaled fixed point format, with 5 digits and the short engineering format is Floating point format, with 5 digits. So when we use only the format function it will changes the data format from short engineering format to the short format.

Code:

clc;
close all;
clear all;
format shortEng
format compact
x = rand(2)
format
x

Output – Command Window

Matlab Format-1.2

After executing the matlab code we get the short engineering format output format with compact line spacing format in the command window.

Example 3:

In this example, we create a variable and display output in the short format and shortG format with loose line spacing for all output lines. Here we have x variable with random outputs display output in the shortG format and short format.so we take x variable. ‘x = [ 35 66.31566 465.55435 99344899999 ]’ his line return x variable with values. Here 35, 66.31566, 465.55435, and 99344899999 this is data is assigned to the variable x. Then we used matlab format function to display output in the short format. After that, we used “format shortG” this syntax of matlab format for display the output in the shortG format with loose line spacing for all output lines

Code:

clc;
close all;
clear all;
x = [35 66.31566 465.55435 99344899999];
format short
x
format shortG
format      loose
x

Output- Command Window

Matlab Format-1.3

After executing the matlab code we get the display output in the short format and shortG format with loose line spacing for all output lines in the command window.

Leave a Reply

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