English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic Grammar of MATLAB

The behavior of the MATLAB environment is like a super complex calculator. You can enter commands at the >> command prompt.

MATLAB is an interpreted environment. In other words, you give a command, and MATLAB executes the command immediately.

Hands-on practice

Enter a valid expression, such as,

5 + 5

Then press ENTER

When you click the Execute button or type Ctrl + When E is used, MATLAB will execute it immediately, and the returned result is-

ans = 10

Let's take a few more examples-

3 ^ 2	       % 3 raised to the power of 2

When you click the Execute button or type Ctrl + When E is used, MATLAB will execute it immediately, and the returned result is-

ans = 9

Another example,

sin(pi /2) % Sine of angle 90o

When you click the Execute button or type Ctrl + When E is used, MATLAB will execute it immediately, and the returned result is-

ans = 1

Another example,

7/0 % Divide by zero

When you click the Execute button or type Ctrl + When E is used, MATLAB will execute it immediately, and the returned result is-

ans = Inf
warning: division by zero

Another example,

732 * 20.3

When you click the Execute button or type Ctrl + When E is used, MATLAB will execute it immediately, and the returned result is-

ans =  1.4860e+04

MATLAB provides some special expressions for certain mathematical symbols, such as pi representing π, Inf representing ∞, i (and j) representing √-1et al.NanRepresents 'not a number'.

Use semicolons (;) in MATLAB

The semicolon (;) indicates the end of a statement. However, if you want to hide and hide the output of an expression in MATLAB, add a semicolon after the expression.

For example,

x = 3;
y = x + 5

When you click the Execute button or type Ctrl + When E is used, MATLAB will execute it immediately, and the returned result is-

y =  8

Add comments

The percentage sign (%) is used to indicate comment lines. For example,

x = 9	     % Assign value9Assign to x

You can also use block comment operators %{} to write comment blocks.

The MATLAB editor includes tools and context menu items that can help you add, delete, or change the format of comments.

Common operators and special characters

MATLAB supports the following commonly used operators and special characters-

OperatorDestination
+

Addition; addition operator.

-

Subtraction; subtraction operator.

*

Scalar and matrix multiplication operator.

.*

Array multiplication operator.

^

Scalar and matrix power operator.

.^

Array power operator.

\

Left division operator.

/

Right division operator.

.\

Array left division operator.

./

Array right division operator.

:

Colon; generate elements with regular spacing and represent entire rows or columns.

( )

Parentheses; enclose function parameters and array indices; rewrite precedence.

[ ]

Brackets; attach array elements.

.

Decimal point.

...

Ellipsis; line continuation operator

,

Comma; separate statements and elements in a row

;

Semicolon; separate columns and suppress display.

%

Percent sign; specify comments and specify format.

_

Reference symbol and transpose operator.

._

Nonconjugated transpose operator.

=

Assignment operator.

Special Variables and Constants

MATLAB supports the following special variables and constants-

NameMeaning
ans

The latest calculated answer.

eps

Floating-point precision.

i,j

Imaginary unit √-1.

Inf

Infinite.

NaN

Undefined numeric results (not numbers).

pi π

Naming Variables

Variable names are composed of letters followed by any number of letters, numbers, or underscores.

MATLAB is case-sensitive.

Variable names can be of any length, but MATLAB only uses the first N characters, where N is given by the function namelengthmax.

Save - save command

saveThe command is used to save all variables in the workspace as files with the .mat extension in the current directory.

For example,

save myfile

You can use it at any time in the futureloadCommand to reload the file.

load myfile