Top Banner
Introduction to MATLAB M. Anand M.Tech. Assistant Professor (Sr. G) Dept. of Information Technology SRM University
58
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Introduction.pdf

Introduction to MATLAB

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

Page 2: 1 Introduction.pdf

Introduction• MATLAB - MATRIX LABORATORY• Developed by MathWorks Inc. • Software package• Provides IDE for programming• Provides predefined functions for

technical computations and visualization

M. Anand 2

Page 3: 1 Introduction.pdf

Introduction (cont)• Uses:

– To perform scientific computations– Visualization– Analyze of various scientific problems

M. Anand 3

Page 4: 1 Introduction.pdf

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 4

Page 5: 1 Introduction.pdf

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 5

Page 6: 1 Introduction.pdf

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 6

Page 7: 1 Introduction.pdf

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 7

Page 8: 1 Introduction.pdf

Introduction (cont)• MATLAB is platform independent• Supported by Windows and

UNIX/LINUX operating systems• MATLAB programs are portable

M. Anand 8

Page 9: 1 Introduction.pdf

Starting a MATLAB Session• Double click on the shortcut on the

desktopOr

• 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 9

Page 10: 1 Introduction.pdf

Ending a MATLAB Session• Select File Exit

Or• Type quit or exit in the command

window

M. Anand 10

Page 11: 1 Introduction.pdf

MATLAB Environment• Components of the MATLAB

environment– Command Window– Command History– Workspace– Current Directory– Figure Window– Edit Window

M. Anand 11

Page 12: 1 Introduction.pdf

M. Anand 12

Page 13: 1 Introduction.pdf

Command Window• Command window is the main

window• Displays the command prompt ‘>>’• Commands are executed

instantaneously>> 30*3+7ans =

97>>

M. Anand 13

Page 14: 1 Introduction.pdf

Command Window• Result stored in a default variable,

‘ans’• Result can be assigned to a variable

>> x = 30 * 3 + 7x =

97>>

M. Anand 14

Page 15: 1 Introduction.pdf

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

Page 16: 1 Introduction.pdf

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;>> zz =

15>>

M. Anand 16

Page 17: 1 Introduction.pdf

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

Page 18: 1 Introduction.pdf

Command History Window• To delete commands right click the

command and select ‘Delete Selection’

M. Anand 18

Page 19: 1 Introduction.pdf

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

Page 20: 1 Introduction.pdf

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

Page 21: 1 Introduction.pdf

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

Page 22: 1 Introduction.pdf

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

Page 23: 1 Introduction.pdf

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

Page 24: 1 Introduction.pdf

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

Page 25: 1 Introduction.pdf

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

Page 26: 1 Introduction.pdf

Figure Window (Cont)• Example:

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

M. Anand 26

Page 27: 1 Introduction.pdf

Figure Window (Cont)

M. Anand 27

Page 28: 1 Introduction.pdf

Help Feature• To get information about any function

or command• Three methods:

1. Help Browser2. help command3. lookfor command

M. Anand 28

Page 29: 1 Introduction.pdf

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

Page 30: 1 Introduction.pdf

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

Page 31: 1 Introduction.pdf

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

Page 32: 1 Introduction.pdf

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

Page 33: 1 Introduction.pdf

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

Page 34: 1 Introduction.pdf

lookfor Command• Useful when one is not sure about

which function perform a particular task

M. Anand 34

Page 35: 1 Introduction.pdf

Type of Files• Three types of files in the MATLAB:

1. M-files2. MAT-files3. MEX-files

M. Anand 35

Page 36: 1 Introduction.pdf

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

Page 37: 1 Introduction.pdf

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

Page 38: 1 Introduction.pdf

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

Page 39: 1 Introduction.pdf

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

Page 40: 1 Introduction.pdf

MAT-files• Syntaxsave <filename> <var1 var2 var3 . . .>

• Load command loads data from the file into the current MATLAB workspace

• Syntaxload <filename>

M. Anand 40

Page 41: 1 Introduction.pdf

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

Page 42: 1 Introduction.pdf

Search Path• The ‘search path’ guides MATLAB to

locate the appropriate program for executing the command

M. Anand 42

Page 43: 1 Introduction.pdf

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

Page 44: 1 Introduction.pdf

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

Page 45: 1 Introduction.pdf

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

Page 46: 1 Introduction.pdf

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 subdirectoriesclick Add with subfolders option

M. Anand 46

Page 47: 1 Introduction.pdf

Search PathThe path-related commands are•addpath

– Adds directory to MATLAB search path•path

– Displays MATLAB search path

M. Anand 47

Page 48: 1 Introduction.pdf

MATLAB Commands1. 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

Page 49: 1 Introduction.pdf

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

Page 50: 1 Introduction.pdf

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

Page 51: 1 Introduction.pdf

MATLAB Commands (Cont)• cd

– Change present working directory (MSDOS)

• copyfile– Copies a file

M. Anand 51

Page 52: 1 Introduction.pdf

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

Page 53: 1 Introduction.pdf

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

Page 54: 1 Introduction.pdf

MATLAB Commands (Cont)• mlock fun

– Locks function fun so that it cannot be deleted

• clc– Clears screen

• clf– Clears Figure Window

M. Anand 54

Page 55: 1 Introduction.pdf

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

Page 56: 1 Introduction.pdf

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

Page 57: 1 Introduction.pdf

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

Page 58: 1 Introduction.pdf

Thank You

M. Anand 58