Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

775420-2-1R3 AID: 232624 |

16/12/2022

Printing to the screen

In MATLAB, there are several ways to print output to the screen. Here are two common
commands for printing to the screen:

1. Using disp command

The disp function is used to display the value of a variable or an expression to the
screen.

 For example, to display the value of a variable x, one can use the following
command:

disp(x)

This will display the value of x to the screen.

 One can also use the disp function to display the result of an expression, such as:

disp(2 + 3)

This will display the result of 2 + 3, which is 5, to the screen.

2. Using fprintf command

The fprintf function is used to display formatted text to the screen. It is similar to the
printf function in C and other programming languages.

 For example, to display a string of text to the screen, one can use the following
command:

fprintf('Hello, world!\n')

This will display the string 'Hello, world!' to the screen, followed by a newline character.

 One can also use the fprintf function to display the value of a variable or the
result of an expression, along with formatting specifiers. For example:

fprintf('The value of x is %d\n', x)

This will display the string 'The value of x is' followed by the value of x, with
the %d formatting specifier indicating that x is an integer.

You might also like