Categories

# Matlab Commands

Matlab provides the commands that are used while the user has to interact with the application using the command line interface. Here we will see how to use those commands with the help of examples

## Basic Commands:

1. Clear – This command removes variables from the memory

2. Exist – This command checks for the existence of a variable

3. Clc – This command clears the command window

4. Global – This command declares a variable as global

5. Help – If you need to search for any help just write the keyword at command window and it displays your available options

6. Who – This command lists the current variable

7. Type – This command displays the contents of a file.

8. Pwd – This command displays the current directory.

9. Date – This command displays the current date

10. What – This command lists all the Matlab files in the current directory

## Example 5:

So if you would clearly observe the example 5 presented above, you can see that the statements hold true and in the last example we called clear command first and thereafter we looked for a type of x and y which now came to be undefined but was not so in example 4.

Note that these examples have been presented with octave that is open source and equivalent platform to Matlab and uses the same command set.

## Intermediate Matlab Commands

Below are the intermediate commands which are as follows:

### 1. Mathematical Functions

Here we are exploring plot command which is being used to plot the sinusoidal wave curve with time, where time is ranging from 1 to 30 and is increasing in intervals of 0.01.

In a similar way, we can do this for any other trigonometric function too which constitutes the major part of mathematics

### 2. Array related Commands

• Cat– It is used to concatenate two arrays, the first argument it takes is a dimension of the array to be created like whether it is 1D,2D or n-dimensional array and thereafter that n-number of arrays are passed as arguments, those are to be concatenated as shown in the snapshot
• Length – Computes the number of elements in the array
• Min, max – These commands return the max and min elements present in the arrays.

### 3. Matrix Related Commands

• Eye – This creates an identity matrix
• Ones – This creates the array of ones
• Zeros – This creates the array of zeros

### 4. Matrix Arithmetic Command

• Dot- This command generates the dot product of two matrices, note that the columns and rows of matrix decide the resulting matrix creation
• Cross – Synonymously this creates the cross product, helpful in vector operations when represented in the matrix form

### 5. Matrix Commands For linear equation related operations

• Rank – This command provides the result to an important mathematical parameter of matrices called rank which is the quantification of the lowest order of non-zero determinant carrying sub-matrix in the parent matrix
• Det – Determinant of the matrix is calculated using this command.
• Inv – Execution of this command provides us the inverse of a matrix.

### 6. Timing related commands

Cputime – Calculates the CPU time in seconds

### 7. String Related commands

• Findstr – Finds occurrences start index of a String in a phrase.
• Strcmp – This command is used to compare two strings, returns 1 if the match is correct else it returns 0.

Let’s take a look at some of the Advanced commands which are as given below:-

### 1. Contour

Whenever you are dealing in control system designs in engineering, the Nyquist plot contours, etc give you a mark of the stability in the system, we can customize the style of the plot in the Matlab (like the color and line type, etc).

### 2. Polynomial related commands

• Poly2sym(vector) – This command converts a vector of coefficients into the corresponding symbolic polynomial (from highest to lowest power).
• poly2sym(vector, ‘v’) – This command converts a vector of coefficients into the corresponding symbolic polynomial inv (from highest to lowest power).

### 3. Commands for Equation solving

syms x1 x2…xn; solve(eq1,eq2,…,eqn, x1, x2,…,xn) – This command solves the simultaneous n number of equations and the solutions against those are assigned to variables ranging from x1,x2 to xn.

The below attached image carries the demonstration

### 4. Commands for solving the Linear System of Equations

X = linsolve (A, B) – Let us suppose that we need to find a solution against the given equation

2x+y+z+m=1

x+2y+z+m=2

x+y+2z+m=3

x+y+z+2m=4

This can be solved in accordance with the attached image

## Tips and Tricks to Use commands

• A programmer shall be aware of use cases like when does Matlab do in-place calculations.
• Use flexible indexing when dealing with tables
• Write Matlab functions with flexible calling Syntax

## Example :

We are supposed to design a function with given syntax

`function a = findArea(width,margin)% findArea(width)% findArea(width,height)% findArea(... 'shape',shape)`

so here, by use of inputParser, you can specify which input arguments are required (width), which are optional (height), and which are optional name-value pairs (‘shape’). inputParser also lets you confirm that each input is valid

As inputParser is a MATLAB object. Make sure to first create an object and then call functions to add the various input arguments.