Categories

# Matlab disp

Disp function is used in MATLAB to display the output of any code without displaying the input variables. This function can be used in cases where our code is not very long or easy to understand, and there is no need of displaying the input variables.

For example, if we use the disp function to display a string ‘MATLAB Disp function’ stored in a variable ‘A’, our output will be ‘MATLAB Disp function’. However, if we do not use the disp function, our output will be A = ‘MATLAB Disp function’.

## Syntax of the disp function:

`disp (A)`

Description of the disp function:

disp (A) function is used to display or print the value stored in the variable ‘A’ without printing the name of the variable

## Examples of Matlab disp

Let us now understand the code to use the disp function in MATLAB.

## Example 1:

In this example, we will use the disp function to display elements of a vector. Below are the steps to be followed:

1. Initialize the input vector
2. Use the disp function to display the elements of the vector

## Code:

``````A = [3 7 1 8 10] [Initializing an input vector and storing it in a variable ‘A’] disp (A)
[Using the disp function to display the value stored in the variable ‘A’]``````

This is how our input and output will look like in MATLAB:

## Output:

As we can see in the output, we have obtained the value (i.e. elements in our case) stored inside the variable ‘A’ by using the disp function.

If we print the output without using the disp function, this is how it will look like:

Please note that in this case, even the variable name is displayed in the output.

## Example 2:

In this example, we will use the disp function to display elements of a matrix. Below are the steps to be followed:

1. Initialize the input matrix
2. Use the disp function to display the elements of the matrix

## Code:

``````A = [3 3 1; 8 0 4; 1 4 9] [Initializing a 3 x 3 input matrix and storing it in a variable ‘A’] disp (A)
[Using the disp function to display the value stored in the variable ‘A’]``````

This is how our input and output will look like in MATLAB:

## Output:

As we can see in the output, we have obtained the value (i.e. elements of the matrix in this example) stored inside the variable ‘A’ by using the disp function.

If we print the output without using the disp function, this is how it will look like:

Please note that in this case, even the variable name is displayed in the output.

## Example 3:

In this example, we will use the disp function to display a string. Below are the steps to be followed:

1. Initialize the input string
2. Use the disp function to display the string

## Code:

``````A = "Let us learn MATLAB disp function"
[Initializing a string and storing it in a variable ‘A’] disp (A)
[Using the disp function to display the value stored in the variable ‘A’]``````

This is how our input and output will look like in MATLAB:

## Output:

As we can see in the output, we have obtained the value (i.e. a string of words in this example) stored inside the variable ‘A’ by using the disp function.

If we print the output without using the disp function, this is how it will look like:

Please note that in this case, even the variable name is displayed in the output.

## Example 4:

In this example, we will use the disp function to display the output of a mathematical expression. Below are the steps to be followed:

1. Initialize the variables involved in the expression
2. Use the disp function to display the output

## Code:

``````X = 5
[Initializing the 1st input variable] Y = 10
[Initializing the 2nd input variable] Z = 25
[Initializing the 3rd input variable] A = X * Z / Y
[Mathematical expression to be solved] disp (A)
[Using the disp function to display the value stored in the variable ‘A’]``````

This is how our input and output will look like in MATLAB:

## Output:

As we can see in the output, we have obtained the value of our output stored inside the variable ‘A’ by using the disp function.

If we print the output without using the disp function, this is how it will look like:

Please note that in this case, even the variable name is displayed in the output.