Anda di halaman 1dari 58

Introduction to MATLAB

M. Anand M.Tech.
Assistant Professor (Sr. G)
Dept. of Information Technology
SRM University

Introduction

MATLAB - MATRIX LABORATORY


Developed by MathWorks Inc.
Software package
Provides IDE for programming
Provides predefined functions for
technical computations and
visualization

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.

A collection of programs are called


Toolboxes
M. Anand

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

Starting a MATLAB Session


Double click on the shortcut on the
desktop
Or
Double click on any MATLAB
generated file (.m, .mat, .mdl, .fig)
Or
Click on the Start Button Select
Programs Select MATLAB
Click on R2008b
M. Anand

Ending a MATLAB Session


Select File Exit
Or
Type quit or exit in the command
window

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

Command History Window


Lists the commands entered in the
Command Window
The commands may be executed by
selecting and double clicking it
A program file may be created by
selecting a set of commands and
right clicking it and selecting Create
M-file option from the pop-up menu
M. Anand

17

Command History Window


To delete commands right click the
command and select Delete
Selection

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

whos var1 var2 . . .


Lists the details of the particular
variables

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

Figure Window (Cont)


Example:
>> x = 0 : 0.05 : 20;
>> y = sin(x);
>> plot (x, y);

M. Anand

26

Figure Window (Cont)

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

MAT-files are binary data-files


.mat extension
Cannot be read by the user directly
Created when the data is saved
using save command
Save command saves data from the
current MATLAB workspace into a
disk file
M. Anand

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.

Note: Use different names for


variables, functions and files
M. Anand

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

MATLAB Commands (Cont)


2. Directory Commands
dir
Lists the files/folders in the Current
Directory (MSDOS)

ls
Lists the files/folders in the Current
Directory (Unix)

pwd
Provides present working directory
(Unix)
M. Anand

49

MATLAB Commands (Cont)


path
Displays MATLAB search path

editpath
Modifies MATLAB search path

mkdir
Makes a directory/folder (MSDOS)
Eg.: mkdir folder1

M. Anand

50

MATLAB Commands (Cont)


cd
Change present working directory
(MSDOS)

copyfile
Copies a file

M. Anand

51

MATLAB Commands (Cont)


3. Workspace Commands
who
Lists the variables currently in the
workspace/memory

whos
Same as who command but gives type
and size

M. Anand

52

MATLAB Commands (Cont)


what
Lists .m, .mat, .mex files on the disk

clear all
Clears the variables in the workspace

clear xyz
Clears the variables specified in the
command

M. Anand

53

MATLAB Commands (Cont)


mlock fun
Locks function fun so that it cannot be
deleted

clc
Clears screen

clf
Clears Figure Window

M. Anand

54

MATLAB Commands (Cont)


4. Termination Commands
Ctrl+c
Stops executing of function/command
being run currently

quit
Quits MATLAB

exit
Same as quit command
M. Anand

55

MATLAB Commands (Cont)


5. help commands
help
Lists the topics for which help is
available

help topic
Provides help for the topic specified in
the command

M. Anand

56

MATLAB Commands (Cont)


lookfor string
Lists the comment line of all .m files
containing the string specified in the
command

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

Anda mungkin juga menyukai