3. Syntax

In previous lesson we learned about MATLAB environment. In this lesson we will learn the syntax. So let’s start.

  • MATLAB is just like a supper fast calculator. You can enter commands in front of these greater than symbol ( >> ) in command prompt. MATLAB can solve it immediately.
  • Type a valid expression in MATLAB, for example, 5 + 5 in command window and press ENTER it immediately returns the result ans = 10 It store the answer in default variable name ans. As shown in picture.

Let us look few more examples. The example given below is according to MATLAB syntax.

  • 1. 2 ^ 3 ( 2 raised to the power of 3 ) ans = 8 2.
  • sin (pi / 2) ( sine of angle 90 ) ans = 1 3.
  • 7/0 ( Divide by zero ) ans = Inf 4.
  • 732 * 20.3 ans = 1.4860 e + 04


  • MATLAB provides some special expressions for some mathematical symbols, like pi for π, Inf for ∞, (i and j) for √-1 etc. Nan stands for ‘not a number’.
  • MATLAB retains your previous keystrokes. Use the up-arrow key to scroll back through the commands. Press the key once to see the previous entry, and so on. Edit a line using the left- and right-arrow keys the Backspace key, and the Delete key.
  • Press the Enter key to execute the command.

Use of Semicolon (;) in MATLAB:

Semicolon (;) indicates end of statement. However, if you want to suppress and hide the MATLAB output for an expression, add a semicolon after the expression. For example,


When you click the Execute button, MATLAB executes it immediately and the result returned is: y = 8 It will not show x = 3.

Comments in MATLAB:

In MATLAB we use percent symbol ( % ) for comments. For example,

x = 9 % assign the value 9 to x

You can also write a block of comments using the block comment operators % { and % }. It start with % { and end with % }. The MATLAB editor includes tools and context menu items to help you add, remove, or change the format of comments.

Arithmetic Operations:

Here are some arithmetic operations.

+ Addition operator
Subtraction operator.
* Scalar and matrix multiplication operator.
.* Array multiplication operator.
^ Scalar and matrix exponentiation operator.
.^ Array exponentiation operator.
\ Left-division operator.
/ Right-division operator.
.\ Array left-division operator.
./ Array right-division operator.
: Generates regularly spaced elements and represents an entire row or column.
( ) Encloses function arguments and array indices; overrides precedence.
[ ] Enclosures array elements
. Decimal point.
Ellipsis. Line-continuation operator
, Separates statements and elements in a row
; Separates columns and suppresses display.
% Designates a comment and specifies formatting.
_ Quote sign and transpose operator.
._ Non conjugated transpose operator.
= Assignment operator.


Order of Precedence:

  • First : Parentheses, evaluated starting with the innermost pair.
  • Second : Exponentiation, evaluated from left to right.
  • Third : Multiplication and division with equal precedence, evaluated from left to right.
  • Fourth : Addition and subtraction with equal precedence, evaluated from left to right.

Examples of Precedence:

Try it yourself in MATLAB




























This is the end of part 1 of Syntax section. Please share this article if you like and do not forget to comment.

This is the second part of our lesson. In this you will learn

  1. Assignment Operator
  2. Commands for managing the work session
  3. Special Variables and Constants
  4. Naming Variables
  5. Complex Number Operations:
  6. Numeric Display Formats and
  7. Mathematical function
  8. Saving your work

So let’s continue…..

1. The Assignment Operator ( = ) :

Typing x = 3 assigns the value 3 to the variable x. We can then type x = x + 2. This assigns the value 3 + 2 = 5 to x. In algebra we can write x + 2 = 20, but in MATLAB we cannot. In MATLAB the left side of the = operator must be a single variable. The right side must be a computable value.

2. Commands for managing the work session:

clc Clears the Command window.
clear Removes all variables from memory.
clear v1 v2 Removes the variables v1 and v2 from memory.
exist ( ‘ var ‘ ) Determines if a file or variable exists having the name ‘var’.
quit Stops MATLAB.
who Lists the variables currently in memory.
whos Lists the current variables and sizes, and indicates if they have imaginary parts.
 exit Exit MATLAB
: Colon; generates an array having regularly spaced elements.


3. Special Variables and Constants:

ans Temporary variable containing the most recent answer.
i , j i , j The imaginary unit √-1.
Inf Infinity.
NaN Not a number. Indicates an undefined numerical result.
pi The number π.
eps Accuracy of floating-point precision.
exp Stand for exponential


4. Naming Variables:

Variable names consist of a letter followed by any number of letters, digits or underscore. MATLAB is case-sensitive. Variable names can be of any length, however, MATLAB uses only first N characters, where N is given by the function namelengthmax.

5. Complex Number Operations:

The number c1 = 1 – 2i is entered as c1 = 1 – 2i. An asterisk is not needed between i or j and a number, although it is required with a variable, such as c2 = 5 – i*c1. Be careful the expressions y = 7 / 2 * i and x = 7 / 2 i give two different results: y = ( 7 / 2 ) i = 3.5i and x = 7 / (2 i) = –3.5i

6. Numeric Display Formats:

format rat Gives the answer in rational form e.g.

format rat 


This gives 1 / 2

format short Four decimal digits (the default); 13.6745.
format long 16 digits; 17.27484029463547.
format short e Five digits (four decimals) plus exponent; 6.3792 e + 03.
format long e 16 digits (15 decimals) plus exponent; 6.379243784781294 e – 04.


7. Mathematical Functions:

Mathematical functions

Note: The MATLAB trigonometric functions use radian measure.

Saving your work:

The save command is used for saving all the variables in the workspace, as a file with .mat extension, in the current directory. For example,

  • save myfile.

You can reload the file anytime later using the load command

  • load myfile

This is the end of our lesson 3.  Please share this article if you like and do not forget to comment.

Note: To ask question you need take this course first and please fill both queston and comment section below. Otherwise your question will not be display. Thank you