You are on page 1of 38

Introduction to MATLAB

KREST TECHNOLOGIES 1
Purpose of this Section
To illustrate simple uses of the MATLAB Technical
language
To help you understand under what circumstances is
MATLAB a better choice than spreadsheets and high-
level languages

KREST TECHNOLOGIES 2
What is MATLAB?
MATLAB (MATrix LABoratory)
MATLAB is developed by The MathWorks, Inc.
MATLAB is a high-level technical computing
language and interactive environment for algorithm
development, data visualization, data analysis, and
numeric computation.
MATLAB can be install on Unix, Windows

3
KREST TECHNOLOGIES
Foundation of Matlab
The name is derived from MATrix LABoratory
MATLAB is a case sensitive language (a variable
named “c” is different than another one called “C”)
MATLAB works with matrices everything
MATLAB understands is a matrix (from text to large
cell arrays and structure arrays)
Various data types exist within MATLAB

KREST TECHNOLOGIES 4
MathWorks Product Overview

KREST TECHNOLOGIES 5
MATLAB
MATLAB is a high-level language and interactive
environment that enables you to perform
computationally intensive tasks faster than with
traditional programming languages such as C, C++,
and Fortran.

KREST TECHNOLOGIES 6
Strengths of MATLAB
MATLAB is relatively easy to learn.
MATLAB code is optimized to be relatively quick
when performing matrix operations.
MATLAB may behave like a calculator or as a
programming language.
MATLAB is interpreted, errors are easier to fix.
Although primarily procedural, MATLAB does have
some object-oriented elements.

KREST TECHNOLOGIES 7
Other Features
2-D and 3-D graphics functions for visualizing data
Tools for building custom graphical user interfaces
Functions for integrating MATLAB based algorithms
with external applications and languages, such as C,
C++, Fortran, Java, COM, and Microsoft Excel

KREST TECHNOLOGIES 8
Weaknesses of MATLAB
MATLAB is NOT a general purpose programming
language.
MATLAB is an interpreted language (making it for
the most part slower than a compiled language such
as C++).
MATLAB is designed for scientific computation and is
not suitable for some things (such as parsing text).

KREST TECHNOLOGIES 9
The Latest Update
The latest version of MATLAB is 7.9
Release 14 with Service Pack 3 (R14SP3), launched
on March, 2009, provides updates to MATLAB,
Simulink, and 75 other products.

KREST TECHNOLOGIES 10
The Matlab Environment

KREST TECHNOLOGIES 11
Cont…
MATLAB has the following basic window
components:
 Work-space
Displays all the defined variables.
 Command Window
To execute commands in the MATLAB
environment
 Command History
Displays record of the commands used

KREST TECHNOLOGIES 12
Command Window

KREST TECHNOLOGIES 13
Command Window (Contd..)
The command window allows you to interact with
MATLAB just as if you type things in a calculator
Cut and paste operations ease the repetition of tasks
Use ‘up-arrow’ key to repeat commands (command
history)

KREST TECHNOLOGIES 14
Matlab Workspace

KREST TECHNOLOGIES 15
Workspace(Contd…)
Workspace of Matlab shows all the defined variables
along with their “order”, memory space they occupy and
the class to which they belong
Above are shown a few such variables

KREST TECHNOLOGIES 16
Current Directory

KREST TECHNOLOGIES 17
Current Directory (Contd…)
Provides quick access to all files available in your Path
Provides a brief description (when files are commented
out) of each M-file

KREST TECHNOLOGIES 18
Different Views

KREST TECHNOLOGIES 19
Different Views (Contd…)
The Matlab environment could be viewed in various
ways.
This could be accomplished by the VIEW pull-down
menu as shown in the previous slide.

KREST TECHNOLOGIES 20
Matlab Help

KREST TECHNOLOGIES 21
Matlab Help (Contd..)
Matlab Help is an extremely powerful assistance to learn
Matlab.
The Help not only contains the theoretical background but
also shows the demos for implementation
The Matlab Help could be opened by using the HELP
pull-down menu

KREST TECHNOLOGIES 22
Matlab Help (Contd..)

KREST TECHNOLOGIES 23
Matlab Help (Contd..)
Any command description can be found by typing the
command in the search field.
As shown above the command to take square root (sqrt) is
searched
We can also utilize the Matlab help from the command
window as shown in the next slide.

KREST TECHNOLOGIES 24
Matlab Help (Contd..)

KREST TECHNOLOGIES 25
To begin a new M-file

KREST TECHNOLOGIES 26
How the source-code looks like

KREST TECHNOLOGIES 27
The Whos Command

KREST TECHNOLOGIES 28
1. clc
clears the command window
2. clear
Removes all variables from the memory.
3. clear var1 var2
Removes the variables var1 and var2 from the memory.
4. exist (‘name’)
Determines if a file or variable exists having the name ‘name’. A 1
indicates that a variable exists and 0 indicates variable does not exists.
5. quit
stops MATLAB.
6. who
List the variables currently in the memory.
7. whos
List the current variables and sizes, and indicates if they have the
imaginary parts.

KREST TECHNOLOGIES 29
Multidimensional Arrays
• A two dimensional array with m rows and n columns
will occupy mxn successive locations in the computer’s
memory. MATLAB always allocates array elements in
column major order.
1
a= [1 2 3; 4 5 6; 7 8 9; 10 11 12];
4
a(5) = a(1,2) = 2 1 2 3 7
4 5 6 10
• A 2x3x2 array of three dimensions
7 8 9 2
c(:, :, 1) = [1 2 3; 4 5 6 ];
10 11 12 5
c(:, :, 2) = [7 8 9; 10 11 12]; 8
• >> x = 1 : 2 : 10; 11
30
KREST TECHNOLOGIES 30
Scalar Calculation

KREST TECHNOLOGIES 31
Vector Calculation

KREST TECHNOLOGIES 32
Matrix Calculation

KREST TECHNOLOGIES 33
Examples
The Command window is where you can interact with
MATLAB directly.
Try typing the following:
foo = ones(2)
The output should look like this:
foo =
11
11
This represents a 2x2 matrix of ones. Matlab inserts
extra blank lines between practically everything. To
turn off this feature, type

KREST TECHNOLOGIES 34
KREST TECHNOLOGIES 35
KREST TECHNOLOGIES 36
KREST TECHNOLOGIES 37
KREST TECHNOLOGIES 38

You might also like