Open In App

MATLAB Commands

Last Updated : 04 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

MATLAB is an interactive multi-programming language and numeric computing environment developed by MathWorks. MATLAB provides the Commands that will be used when the user wants to interact with any application using the command line interface. 

Following are the lists of commands used in MATLAB.

Commands for Managing a Session

Command 

Use

clcClears command window
clearRemoves the variables from memory
globalDeclares variables globally
existChecks for the existence of a particular file or memory
helpSearches for a help topic
quitStops MATLAB
whoLists current variables in use
lookforSearches help entries for a keyboard

Example 1

Matlab
% MATLAB Code to illustrate
% global
     function setGlobalx(val)
    global x

Output:

x = val;

Example 2

Matlab
% MATLAB Code to illustrate who
    x = 2
    who

Output:

Variables in the current scope: x

 Example 3

Matlab
% MATLAB Code to illustrate
% clear
    clear
    type x

Output:

error: type 'x' is undefined

Commands for Working with the System

Command Use
cdChanges current directory
date Displays current date
deleteDeletes a file
dirLists all the files in a directory
pathDisplays search path
pwdDisplays current directory
typeDisplays content of a file
wklreadReads.wk1 spreadsheet file
saveSaves workplace variables in a file

Example 1 

Matlab
% MATLAB Code to illustrate
% pwd
    pwd

Output:

ans = /home/oo

Example 2

Matlab
% MATLAB Code to illustrate
% date
c = date

Output:

c= '18-jun-2021'

Input and Output Commands

Command Use
dispDisplays content of an array or a string
fscanfReads formatted data from a file
formatControls screen-display format
fprintfPerforms formatted writes to file or screen 
inputDisplays the prompt and waits for input
;Suppresses screen printing

Example 1 

Matlab
% MATLAB Code to illustrate
% disp

    A = [15 150];
    S = 'Hello World.';
    disp(A)
    disp(S)

Output:

15 150
    
Hello World.

Example 2

Matlab
% MATLAB Code to illustrate
%input
 age = input('how old are you: '); 
     % At this point, the variable: age, will contain 
     % whatever value the user types 

Output:

20
        

Vector, Matrix, and Array Commands

Command Use
cat Concatenates the array
findFinds the indices of nonzero elements
max Returns the largest element
minReturns the smallest element
prodProduct of each column
sortSorts each column
rankComputes rank of a matrix
eyeCreates an identity matrix
inv Computes the inverse of a matrix

Example 1

Matlab
% MATLAB Code to illustrate 
%cat
    array1 = [1,2,3]
    array2 = [4,5,6]
    cat(1,array1,array2)

Output:

    ans = 
    
    1    2    3
    4    5    6

Example 2

Matlab
% MATLAB Code to illustrate 
%max
max(array1)

Output:

ans = 3

Example 3

Matlab
%MATLAB Code to illustrate
%min
min(array2)

Output:

ans = 4
Matlab
%MATLAB Code to illustrate
%eye
eye(2,2)

Output:

    ans = 
    Diagonal Matrix
    1    0
    0    1

Plotting Commands: 

Commands Use
axisSets axis limit
gridDisplays grid lines
plotGenerates xy plot
title Puts title at top of the plot
close Closes current plot
barCreates bar chart
print Prints the plot / saves the plot
figureOpens a new figure window
Close allCloses all plots

Example1:

Matlab
% MATLAB Code to illustrate
%plot
    x = linspace(0,20)
    y = cos(x)
    plot(x,y)

Output:

Example 2:

Matlab
%MATLAB Code to illustrate
%grid
    x = linspace(0,20)
    y = cos(x)
    plot(x,y)
    grid on

Output:


Next Article
Article Tags :

Similar Reads