MATLAB is a programming language which can be used for numerical computations, matrix operation, simulation, prototyping, Graphical User Interface and a lot more. In this tutorial we are going to get familiar with MATLAB, MATLAB commands and a small program to wind up with this tutorial.

**Step 1: Introduction**

*Fig. 1: Screenshot of Matlab IDE on Windows*

This is the MATLAB environment which we experience once we open MATLAB. The arrows indicate the various windows like command window, workspace and the command history.

**Command window**– This is the window in which one can type one line command and obtain its result, also when any program is run the output is viewed in this window.

**Workspace**– This is the window where all the variables which we have defined in a program and also the variables which are included in a function are displayed and stored in the memory.

**Command History**– This is the window in which all the statements which we had run in the previous and the current MATLAB sessions are displayed.

**Step 2: Command window**

Now let’s try some examples to get a better understanding of MATLAB.

Type a=2 in the command window and view the result.

*Fig. 2: Screenshot of Command Window in Matlab IDE*

You can just look at the workspace window where a variable ‘a’ with a value 2 is displayed and stored in the memory. The command history window displays all the statements which I have typed during the current session and also the statements from previous session. The command window shows variable ‘a’ with the assigned value 2.

Now let’s try an example by just typing 2.

*Fig. 3: Screenshot of Variable Declaration on Matlab Editor*

If you just type a number 2 it gets stored in a default variable which MATLAB uses i.e. ans. It happens when we have not defined an output variable. Therefore the values which have no output variable, it gets stored in ans and so the value of ans keeps on changing.

Now try a=2 with a semicolon i.e. a=2;

*Fig. 4: Screenshot showing working of Default variables in Matlab*

## What do you observe?

If any statement ends with a semicolonit only gets stored in the output variable and is not displayed in the command window.

All the values which we assign to a variable are stored in a matrix form in MATLAB. This can be checked by typing a(1,1) which means value that is stored at 1^{st} row and 1^{st} column.The result is 2 because this is the only value that is stored in variable ‘a’ which means it is at 1^{st} row and 1^{st} column place.

*Fig. 5: Screenshot of Command Window in Matlab IDE showing program outputs*

What if we type a(1,2) or a(2,1)?

*Fig. 6: Screenshot showing error messages on Command Window in Matlab IDE*

This is because only a single value was stored in the variable and so this error message is displayed

Now let’s get familiar with some of the basic commands of MATLAB-

clc- It clears the entire command window.

clear all- It clears all the variables which are stored in memory.

disp()- It displays the string which we type within a single inverted commas in the round brackets. E.g. disp(‘Test’).

quit- It closes MATLAB.

Let’s try some more examples:

c=2+3

d=2*3

e=2-3

f=2/3

a= [1 2 3 4]

b= [1 2 3 4; 5 6 7 8]

*Fig. 7: **Screenshot showing a simple Matlab Program running on Command Window*

**Step 3: M-file**

A basic MATLAB program is called a MATLAB script with .m extension.

We will write a short program in this section to better understand MATLAB but before starting with the program let’s understand loop control statements of MATLAB.

There are two loops in MATLAB: for loop and while loop

**for i=1:1:9**

**disp(‘Test’);**

**end**

This loop will start displaying ‘Test’ for nine times with ‘i’ starting from 1 and getting incremented by 1 after every iteration and stops as it reaches 9.

**i=1;**

**while(i<=1)**

**disp(‘Test’);**

**i=i+1;**

**end**

This loop will start its execution by first checking whether the value of i is 1 or less than 1. Here i=1 so the loop starts itsexecution but after displaying once, the value of i changes due to i=i+1 and the loop comes to an end.

This is the way how we define a loop in MATLAB.

Now let’s start with the program. Since it’s a basic tutorial on MATLAB, we are going to write a simple program with the commands we saw in this tutorial.

Click on New à Script and start writing the program.

After writing the program save the program by clicking Save As and type the name of the program.

The execution of a program can be started by clicking on the run button in the window where you have typed the program or you can press F5 or you can also write the name of the program in command window with which you have saved it and press enter.

**Program:**

clc;

clear all;

for i=1:1:2

a= input(‘Enter the first number = ‘);

b=input(‘Enter the second number = ‘);

c=a+b;

disp(‘Addition result = ‘); disp(c)

d=a-b;

disp(‘Subtraction result = ‘); disp(d)

e=a*b;

disp(‘Multiplication result = ‘); disp(e)

f=a/b;

disp(‘Division result = ‘); disp(f)

end

**Explanation:**

‘clc’ will clear the command window and ‘clear all’ will erase all the variables.

Then the program starts with a for loop and since the value of i starts from 1 and ends at 2 the entire program from the for loop will execute twice.

input (‘Enter the first number = ’) will ask the user to type a number. All the other commands are the ones with which you all might be familiar by now.

*Fig. 8: Screenshot of a Matlab Program as M-File*

*Fig. 9: Screenshot showing Matlab Program running on Command Window*

This is the output of the program which we have written and because of for loop the execution of the program from for loop was done twice.

Hope this tutorial made you to learn something about MATLAB.

Filed Under: Electronic Projects

## Questions related to this article?

👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.

Tell Us What You Think!!

You must be logged in to post a comment.