# Syntax in MATLAB

**MATLAB Syntax**: Here, we are going to learn about the **syntax of MATLAB**, and **notes for working in the command window**.

Submitted by **Alok Kumar**, on September 09, 2019

The **syntax** is the method by which the programmer give various commands to the system or the software. These commands are given to perform the specific task like.

In C OR C++, we give a command like #include<stdio.h> ,as it is giving command to the compiler to add the header file name stdio.h to the program or to execute it in the same way there are various syntax present in the MATLAB to give instruction to the software to perform the specific tasks.

First, when we open the MATLAB software it seems very complex to use as a calculator but we can easily use the software if we know the set of rules through which we can easily handle it.

**Notes for working in the command window**

- To type a command the cursor must be placed next to the command prompt (>>).
- Once a command is typed and the enter key is pressed, the command is executed. However, only the last command is executed. Everything executed previously is unchanged.
- Several commands can be typed in the same line. This is done by typing a command between the commands. When the enter key is pressed the commands are executed in order from left to right.
- It is not possible to go back to a previous line in the command window, make a correction, and then re-executed the command.
- A previously typed command can be recalled to the command prompt with the up-arrow key. When the command is displayed at the command prompt, it can be modified if needed and executed. The down-arrow key can be used to move down the previously typed commands.
- If a command is too long to fit in one line, it can be contained to the next line by typing the periods… (called an ellipsis) and pressing the enter key. The continuation of the command is then typed in the new line. The command can continue line after line up to a total of 4096 characters.

**Way to execute the program in MATLAB**

Type a valid expression, for example: 6+6 And press ENTER

When you click execute button or type **CTRL+E**, MATLAB executes it immediately and the result will return as,

Ans =12

In the same way we can execute various commands like,

4^5 power of 5 raised to 4 Cos (pi/2) cos of angle 90 degree 567*678 first number multiplied to the second

Above are some ways to give the command to **MATLAB software** and then press **ENTER** or **CTRL+E** to execute and after pressing the execution button it will show the result.

**MATLAB** provides some special expressions for some mathematical symbols, like **pi** for **22/7**, **inf** for infinity **I** (or **j**) for **sqrt(-1)**.

There are also various symbols used in MATLAB which act as syntax like given below,

- (
**>>**) The point from where we can start to write our program. - (
**;**) If a semicolon is typed at the end of a command the output of the command is not displayed. It is useful when the result is known. - (
**%**) When this symbol is typed at the beginning of a line, the line is designated as a comment.

**Example to execute a program**

>> 7 + 8/2 % type and press enter % Ans: 11 % 8/2 is executed first %

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.