0
0
MatlabHow-ToBeginner ยท 3 min read

How to Use Semicolon in MATLAB: Syntax and Examples

In MATLAB, use a semicolon (;) at the end of a statement to suppress the output from displaying in the command window. You can also use semicolon (;) to separate multiple commands on the same line.
๐Ÿ“

Syntax

The semicolon in MATLAB is used in two main ways:

  • At the end of a statement to suppress output.
  • Between commands on the same line to separate them.

Example syntax:

x = 5; % Assign 5 to x and suppress output
y = 10; z = x + y; % Two commands separated by semicolon
matlab
x = 5; % Assign 5 to x and suppress output
y = 10; z = x + y; % Two commands separated by semicolon
๐Ÿ’ป

Example

This example shows how using a semicolon suppresses output and how multiple commands can be written on one line separated by semicolons.

matlab
a = 3; % No output shown
b = 4; c = a * b; % Two commands on one line
c % Display value of c
Output
c = 12
โš ๏ธ

Common Pitfalls

Common mistakes include forgetting the semicolon, which causes MATLAB to display output unnecessarily, cluttering the command window. Another mistake is using commas instead of semicolons to separate commands on the same line, which is invalid syntax.

matlab
x = 7 % Missing semicolon, output will show
x = 7; y = 8, z = x + y; % Incorrect use of comma
x = 7; y = 8; z = x + y; % Correct use of semicolons
Output
x = 7 Error: Unexpected MATLAB expression. z = 15
๐Ÿ“Š

Quick Reference

Summary tips for using semicolons in MATLAB:

  • Use ; at the end of a line to hide output.
  • Use ; to separate multiple commands on one line.
  • Omitting ; shows the result in the command window.
  • Do not use commas to separate commands.
โœ…

Key Takeaways

Use a semicolon at the end of a statement to suppress output in MATLAB.
Separate multiple commands on the same line with semicolons, not commas.
Omitting the semicolon causes MATLAB to display the result of the statement.
Proper use of semicolons keeps the command window clean and improves script readability.