M. Anand M.Tech.
Assistant Professor (Sr. G)
Dept. of Information Technology
SRM University
Introduction
M. Anand
Introduction (cont)
Uses:
To perform scientific computations
Visualization
Analyze of various scientific problems
M. Anand
Introduction (cont)
Provides a collection of functions
(tools) for specific applications
Image processing
Neural Networks
Control System
Digital Signal Processing, etc.
Introduction (cont)
The basic building block in MATLAB
is matrix
Basic data type(matrix) is defined as
an array
Vectors, scalars etc. are
automatically handled as matrices
M. Anand
Introduction (cont)
Scalars are treated as a matrix of
single row and single column
A single row matrix is treated as row
vector
A single-column matrix are treated
as column vector
M. Anand
Introduction (cont)
No need to declare the dimensions
of a matrix
No need to declare any variables
and their types
MATLAB automatically sets the type
of the variable according to the type
of value assigned to that variable
M. Anand
Introduction (cont)
MATLAB is platform independent
Supported by Windows and
UNIX/LINUX operating systems
MATLAB programs are portable
M. Anand
M. Anand
10
MATLAB Environment
Components of the MATLAB
environment
Command Window
Command History
Workspace
Current Directory
Figure Window
Edit Window
M. Anand
11
M. Anand
12
Command Window
Command window is the main
window
Displays the command prompt >>
Commands are executed
instantaneously
>> 30*3+7
ans =
97
>>
M. Anand
13
Command Window
Result stored in a default variable,
ans
Result can be assigned to a variable
>> x = 30 * 3 + 7
x=
97
>>
M. Anand
14
Command Window
If the output of a statement is not
needed to be displayed, add ; at the
end of the statement
>> x = 30 * 3 + 7 ;
>>
M. Anand
15
Command Window
Find the value of z for the expression
z = x * y, if x = 5 and y = 3
>> x=5;
>> y=3;
>> z=x*y;
>> z
z=
15
>>
M. Anand
16
17
M. Anand
18
Workspace
Lists collection of all the variables in
the current MATLAB session
Shows their data type and size
Commands to get the workspace
information
M. Anand
19
Workspace (Cont)
who
Lists all the variables in the workspace
whos
Lists all the variables in the workspace
with their size, number of bytes, and
class/type of variable
whos global
Lists the variables in the global
workspace
M. Anand
20
Workspace (Cont)
whos file filename
Lists the variables in the specified .mat
file
M. Anand
21
Current Directory
Lists all the files and folders present
in the current directory
To run any file, it must either be in
the Current Directory or on the
search path
M. Anand
22
Edit Window
Used to create a new program file
Extension (.m)
To create a new M-File
File New M-File
To open an existing M-File
File Open
To save the content
File Save
M. Anand
23
Edit Window
The saved M-file is executed by
typing its name without the extension
in the Command window
Edit Window can also be used to
debug programs
M. Anand
24
Figure Window
Used to display MATLAB graphics
Results of all the graphic commands
are displayed in the Figure Window
Any number of Figure windows can
be opened depending upon the
system memory
M. Anand
25
M. Anand
26
M. Anand
27
Help Feature
To get information about any function
or command
Three methods:
1. Help Browser
2. help command
3. lookfor command
M. Anand
28
Help Browser
Select the Help icon from the toolbar
Or
Choose Help menu
Or
Type doc or helpdesk or helpwin in
the command window
M. Anand
29
help Command
To obtain help for any particular
command
help command searches for an exact
function name match
>> help sin
Help command is useful when the
name of the function is known
M. Anand
30
help Command
>> help
Displays a list of all help topics and
different toolboxes installed in the
system
>> help help
Displays help about the help
M. Anand
31
help Command
To list all the commands available in
the toolbox, use help followed by the
name of the toolbox
>> help control systems
M. Anand
32
lookfor Command
To obtain help on a particular topic
Searches the string in the first
command line of the help text in all
the M-files found on the MATLAB
path
Slower than the help command
Provides more detailed useful
information
M. Anand
33
lookfor Command
Useful when one is not sure about
which function perform a particular
task
M. Anand
34
Type of Files
Three types of files in the MATLAB:
1. M-files
2. MAT-files
3. MEX-files
M. Anand
35
M-Files
Standard ASCII text files, with a (.m)
extension
Program written in the Edit window is
saved as M-files
Types of M-files
Script files
Function files
M. Anand
36
Script Files
An M-file with a set of valid MATLAB
commands is called a script file
To run a script file, the filename
without the (.m) is entered in the
command window
The script files work on global
variables, that is, the variables
currently present in the workspace
M. Anand
37
Function Files
An M-file which begins with a
function definition line is called a
function file
If a file does not begin with function
definition line, it becomes a script file
Function files can be called from the
script files or another function file
M. Anand
38
MAT-files
39
MAT-files
Syntax
save <filename> <var1 var2 var3 . . .>
Load command loads data from the
file into the current MATLAB
workspace
Syntax
load <filename>
M. Anand
40
MEX-files
MEX-file is MATLAB callable
FORTRAN and C program, with a
(.mex) extension
Allows the user to integrate the code
written in FORTRAN or C language
into the programs developed using
MATLAB
M. Anand
41
Search Path
The search path guides MATLAB to
locate the appropriate program for
executing the command
M. Anand
42
Search Path
Whenever a word is entered at the
command prompt, the MATLAB
follows the following steps in order to
interpret it correctly:
First, it is assumed that the word is a
variable, if found in workspace,
MATLAB displays its contents in the
command window
M. Anand
43
Search Path
If it is not a variable name, it is
assumed as a built-in function or
command, if found, it is executed by the
MATLAB
If it is not a built-in function or
command, then MATLAB checks the
Current Directory if there is an M-file by
that name. If found, it is executed
M. Anand
44
Search Path
If it is not present in Current Directory
then MATLAB searches for the M-file in
all folders and directories in the
sequence given on the MATLAB search
path. If found, it is executed
If the word does not match anywhere, it
responds by indicating that such a
function or variable is not defined.
45
Search Path
To execute a script file, it must be
included in Current Directory or
search path of MATLAB
To add the folder in the search path
select File Set Path
To add the selected directory and all
of its subdirectories
click Add with subfolders option
M. Anand
46
Search Path
The path-related commands are
addpath
Adds directory to MATLAB search path
path
Displays MATLAB search path
M. Anand
47
MATLAB Commands
1. General Commands
clock
Provides clock time and date as a
vector
date
Provides date as a string
ver
Gives the version of MATLAB installed
M. Anand
48
ls
Lists the files/folders in the Current
Directory (Unix)
pwd
Provides present working directory
(Unix)
M. Anand
49
editpath
Modifies MATLAB search path
mkdir
Makes a directory/folder (MSDOS)
Eg.: mkdir folder1
M. Anand
50
copyfile
Copies a file
M. Anand
51
whos
Same as who command but gives type
and size
M. Anand
52
clear all
Clears the variables in the workspace
clear xyz
Clears the variables specified in the
command
M. Anand
53
clc
Clears screen
clf
Clears Figure Window
M. Anand
54
quit
Quits MATLAB
exit
Same as quit command
M. Anand
55
help topic
Provides help for the topic specified in
the command
M. Anand
56
helpwin
Opens the help window
helpdesk
Opens the web browser for online help
demo
Runs the demo program
M. Anand
57
Thank You
M. Anand
58