Matlab

MATLAB Basic Commands and How to use them, explained with Examples

MATLAB Basic Commands, Overview:

Matlab Basic Commands and how to use them– Before you start writing complex scripts using the MATLAB Software you should know about the basic commands and how to use them properly. Because, these basic commands are used in complex MATLAB projects. If you are a beginner and you have just started learning the MATLAB, then trust me this article is for you. Without any further delay, let’s get started!!!



MATLAB Basic Commands

Who:

To discuss the who command we will first declare several variables:

A= [1 2 3]

B= [4 5 6]

C = [7 8 9]

The “who” command will list variables in the work space. To implement this command we will write who in the command window.

Matlab Basic Commands

It will show all the variables in the workspace that we have declared for example A, B and C.

Whos:

The whos command will list the variables currently in the workspace with their types. It will show size, bytes, and class of the variables in the workspace. To implement this command we will write whos in the command window and press enter and will show the output.

Matlab Basic Commands

What:

The what command will show the MATLAB code files like m-files in the current folder. For example in our case there is only one m- file with the name formulae equation.

Matlab Basic Commands

Clear:

In this command all the variables assigned in the workspace will be cleared. For example we have assign variables A, B, and C which are shown in the workspace.

Matlab Basic Commands

Now we want to clear all these variables we write the clear command in command window and all the variables will be removed from the workspace.

Matlab Basic Commands




Clear x:

This command will remove only one variable from workspace. For example we will declare three variable A, B and C.

Matlab Basic Commands

Now we want to remove only variable A we will write the command clear A

Matlab Basic Commands

Now we can see that “A” variable is removed from the workspace.

Clc:

The clc command will clear the screen.

Matlab Basic Commands

Now after implementing this command by pressing enter the screen will be cleared.

Matlab Basic Commands

Clf:

Clf means clear figure this command will clear the figure. For example we have figure:

n=[0:39];

f=50;

fs=1000

x=sin(2*pi*(f/fs)*n);

plot(n,x)

Matlab Basic Commands

Now we want to clear this figure we will write the command clf and the waveform will  be removed from the figure.

Matlab Basic Commands



Pwd:

This command will gives us the information of the directory. By implementing this command it will show the current working directory.

Matlab Basic Commands

Dir:

This command will give the current directory and will list the content s of the directory. To implement this command we will write dir in the command window and press eneter:

Matlab Basic Commands

Path:

This command will give the sketch path of the MATLAB.

Matlab Basic Commands

Computer:

This command will give the information of the computer that we are using.

Matlab Basic Commands

So this shows that I am using Window 64 bit.

Date:

This command will show us the date.

Matlab Basic Commands

Clock:

The clock command will show the current time and data. To implement this command we will write the clock in the command window.

Matlab Basic Commands

Now in the above command output:

  • 0210 shows that the year is 2021
  • 0040 show that the current month number is 4
  • 0210 shows that the data is 21
  • 0220 shows the hour which is equal to 10 P.M
  • 044 shows the minutes which are 44 minutes
  • 0122 shows the seconds which are 12 seconds



Ver:

This command will show the current version of the MATLAB that you are using

Matlab Basic Commands

Rand(x):

This command will generate the random values for example if want to generate random values have 6 row and 6 columns we will write the command.

Rand(6)

Matlab Basic Commands

Xlabel:

The x-axis can also be labeled by using the command xlabel this command will label the X-axis for example we have sine wave and we want to label it axis as time then we write the command:

xlabel(‘time’)

n=[0:39];

f=50;

fs=1000

x=sin(2*pi*(f/fs)*n);

plot(n,x)

xlabel(‘time’)

Matlab Basic Commands

Ylabel:

Similarly the  y-axis can also be labeled by using the command ylabel. This command will label the y-axis for example we have sine wave and we want to label it y-axis as amplitude then we write the command:

ylabel(‘Amplitude’)

n=[0:39];

f=50;

fs=1000

x=sin(2*pi*(f/fs)*n);

plot(n,x)

xlabel(‘time’)

ylabel(‘amplitude’)

Matlab Basic Commands



Title:

A title can also be given to the plot by using the title command.  For example we have sine wave and we want to give its title then we will write the command.

title(‘this is sine wave’)

n=[0:39];

f=50;

fs=1000

x=sin(2*pi*(f/fs)*n);

plot(n,x)

xlabel(‘time’)

ylabel(‘amplitude’)

title(‘this is sine wave’)

Matlab Basic Commands

Diary:

The diary command is used to keep track of everything done during a MATLAB session. It will track all the record from the start to end of the MATLAB until the diary command is on. It will record all the commands which we will used in the command window. Now to use this command we will write the diary and then file name with which we want to save our data. For example we want to save data with the file name a1.

diary a1

Matlab Basic Commands

If you implement diary command and it show this problem then you should need to change the directory such that:

Matlab Basic Commands

Now when we change the directory and implement the diary command it will be working and it will create the file in the current folder.  You can also change the directory by using the following method and select the folder where you want to save the files.

Matlab Basic Commands

By clicking the browse folder another dialog box will appear where we will select the folder to save the files.

Matlab Basic Commands

Now the directory will be change and we will implement the diary command:

Matlab Basic Commands

Now you can see that by changing the directory the diary command is implemented and a1 file is created. Now we will write some variables in order to save it in the a1 file.

Matlab Basic Commands

Now to see your work just double  click on the a1 file and all the data will be appear in form of m-file.

Matlab Basic Commands

So all the work is saved.

Now if want to close the diary such that further command will not save in the file we will write the command diary off.

Diary off

Now we will write several commands to look that whether these commands are save in the a1 file or not we will declare several variables like:

X=[2 3]

Y=[6 7]

Matlab Basic Commands

Now to check the diary file we will again double click on the a1 file:

Matlab Basic Commands

Now we can see that no variables are save in the file after diary is off.

If we want to turn on diary again we will write the command diary on and will declare several variables.

Matlab Basic Commands

Now again click on a1 file in order to see whether the commands or save in it or not.

Matlab Basic Commands

Now we can see that the commands are saved after the diary is turned on again.




Exit:

When we write the exit or quit command in the command window the matlab will be closed.

Matlab Basic Commands

Special commands for the generation of matrices:

Ones (2,2):

This command will generate a matrix of ones having two rows and two columns.

Matlab Basic Commands

Zeros (2,2):

This command will generate a matrix of zeros having two rows and two columns.

Matlab Basic Commands



Eye (4):

This command will generate identity matrix having 4 rows and 4 columns .

Matlab Basic Commands

Generation of the Diagonal matrix:

If we want create a diagonal matrix for example have matrix A= [1 2 3 4] then to create a diagonal matrix from it we will implement the command:

A=diag(v)

Matlab Basic Commands

Now if we want to generates square matrix with elements of A placed in second diagonal above the main diagonal. Then we will write the command:

Diag(A,2)

Matlab Basic Commands

Now if we want to generates a square matrix with elements of A placed in second diagonal below the main diagonal then we will write the command:

Diag(A,-2)

Matlab Basic Commands

Now if we want to generates a square matrix with elements of A placed in third diagonal below the main diagonal we will write the command diag(A,-3)

Matlab Basic Commands



Block diagonal matrix:

blkdiag  command is the Block diagonal concatenation of matrix input arguments.

For example if we have three matrix:

A = [1 2 3 4 5]

B = [6 7 8 9 10]

C = [11 12 13 14 15]

We will write the command:

blkdiag(A,B,C)

Matlab Basic Commands

Generation of Upper triangular matrix:

TRIU (x):

This command will extract upper triangular part of the matrix. For example we will create 6 by 6 matrix of random values by using the command:

X=rand(6)

Now to create the upper triangular matrix from these values we will write the command

Triu(x)

Matlab Basic Commands



TRIU(X,K):

This command will generates the elements on and above the K diagonal of X. Where k=0 is the main diagonal K>0 is above the diagonal and K < 0 is below the main diagonal.

Now if we want to extract the elements on and above the third diagonal of x we will write the command.

Triu(x,3)

Matlab Basic Commands

Now to extract the element above the (-1)th diagonal of A we will write the command

Triu(x,-1)

Matlab Basic Commands

TRIL (x):

This command will extract the lower triangular part of x.

Matlab Basic Commands



TRIL(X,K):

This command will generates the elements on and below the K diagonal of X. Where k=0 is the main diagonal K>0 is above the diagonal and K < 0 is below the main diagonal.

Now if we want to extract the elements on below the third diagonal of x we will write the command.

Tril(x,3)

Matlab Basic Commands

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button