CH1
CH1
Features of MATLAB
Following are the basic features of MATLAB −
It application It is a high-level language for numerical computation, visualization and
application development.
It provides built-in graphics for visualizing data and tools for creating custom plots.
Uses of MATLAB
the MATLAB is widely used as a computational tool in science and engineering
encompassing the fields of fields of physics, chemistry, math and all engineering streams. It
is used in a range of applications including −
Signal Communications Signal Processing and Communications
Image Processing Image and Video Processing
Control Systems Control Systems
Test Measurement Test and Measurement
Computational Finance Computational Finance
Computational Biology Computational Biology
Command Window − This is the main area where commands
can be entered at the command line. It is indicated by the
command prompt (>>).
Workspace − The workspace shows all the variables created
and/or imported from files.
Hands on Practice
Type a valid expression, for example,
5 + 5
ans = 10
ans = 9
Another example,
ans = Inf
warning: division by zero
Another example,
732 * 20.3
ans = 1.4860e+04
For example,
x = 3;
y = x + 5
y= 8
Adding Comments
The percent symbol (%) is used for indicating a comment line. For
example
You can also write a block of comments using the block comment
operators % { and % }.
The MATLAB editor includes tools and context menu items to help
you add, remove, or change the format of comments.
Operator Purpose
\ Left-division operator.
/ Right-division operator.
. Decimal point.
= Assignment operator.
Inf Infinity.
pi The number π
Naming Variables
Variable names consist of a letter followed by any number of letters,
digits or underscore.
MATLAB is case-sensitive.
Variable names can be of any length, however, MATLAB uses only
first N characters, where N is given by the
function namelengthmax.
For example,
save myfile
You can reload the file anytime later using the load command.
load myfile
x=3
MATLAB will execute the above statement and return the following
result −
x=4
MATLAB will execute the above statement and return the following
result −
MATLAB will execute the above statement and return the following
result −
Long Assignments
Long assignments can be extended to another line by using an
ellipsis (...). For example,
initial_velocity = 0;
acceleration = 9.8;
time = 20;
final velocity = initial_velocity + acceleration * time
MATLAB will execute the above statement and return the following
result −
final_velocity = 196
For example −
format long
x = 7 + 10/3 + 5 ^ 1.2
MATLAB will execute the above statement and return the following
result−
x = 17.2319816406394
Another example,
format short
x = 7 + 10/3 + 5 ^ 1.2
MATLAB will execute the above statement and return the following
result −
x = 17.232
format bank
daily_wage = 177.45;
weekly_wage = daily_wage * 6
MATLAB will execute the above statement and return the following
result −
weekly_wage = 1064.70
For example,
format short e
4.678 * 4.9
MATLAB will execute the above statement and return the following
result −
ans = 2.2922e+01
format long e
x = pi
MATLAB will execute the above statement and return the following
result −
x = 3.141592653589793e+00
format rat
4.678 * 4.9
MATLAB will execute the above statement and return the following
result −
ans = 34177/1491
Creating Vectors
A vector is a one-dimensional array of numbers. MATLAB allows
creating two types of vectors −
Row vectors
Column vectors
Row vectors are created by enclosing the set of elements in square
brackets, using space or comma to delimit the elements.
For example,
r = [7 8 9 10 11]
MATLAB will execute the above statement and return the following
result −
r=
7 8 9 10 11
Another example,
r = [7 8 9 10 11];
t = [2, 3, 4, 5, 6];
res = r + t
MATLAB will execute the above statement and return the following
result −
res =
9 11 13 15 17
MATLAB will execute the above statement and return the following
result −
c=
7
8
9
10
11
Creating Matrices
A matrix is a two-dimensional array of numbers.
m = [1 2 3; 4 5 6; 7 8 9]
MATLAB will execute the above statement and return the following
result −
m=
1 2 3
4 5 6
7 8 9
MATLAB is an interactive program for numerical computation and
data visualization. You can enter a command by typing it at the
MATLAB prompt '>>' on the Command Window.
Command Purpose
Command Purpose
Command Purpose
The fscanf and fprintf commands behave like C scanf and printf
functions. They support the following format codes −
%s Format as a string.
%d Format as an integer.
The format function has the following forms used for numeric
display −
Format
Display up to
Function
Command Purpose
Plotting Commands
MATLAB provides numerous commands for plotting graphs. The
following table shows some of the commonly used commands for
plotting −
Command Purpose
The above command will create the file in default MATLAB directory.
If you want to store all program files in a specific folder, then you
will have to provide the entire path.
If you are creating the file for first time, MATLAB prompts you to
confirm it.
Alternatively, if you are using the IDE, choose NEW -> Script. This
also opens the editor and creates a file named Untitled. You can
name and save the file after typing the code.
NoOfStudents = 6000;
TeachingStaff = 150;
NonTeachingStaff = 20;
After creating and saving the file, you can run it in two ways −
a = 5; b = 7;
c = a + b
d = c + sin(b)
e = 5 * d
f = exp(-d)
c = 12
d = 12.657
e = 63.285
f= 3.1852e-06
For example,
Total = 42
Sr.N
Data Type & Description
o.
1 int8
8-bit signed integer
2 uint8
8-bit unsigned integer
3 int16
16-bit signed integer
4 uint16
16-bit unsigned integer
5 int32
32-bit signed integer
6 uint32
32-bit unsigned integer
7 int64
64-bit signed integer
8 uint64
64-bit unsigned integer
9 single
single precision numerical data
10 double
double precision numerical data
11 logical
logical values of 1 or 0, represent true and false respectively
12 char
character data (strings are stored as vector of characters)
cell array
13 array of indexed cells, each capable of storing an array of a different dimension and data
type
structure
14 C-like structures, each structure having named fields capable of storing an array of a
different dimension and data type
15 function handle
pointer to a function
16 user classes
objects constructed from a user-defined class
17 java classes
objects constructed from a Java class
Example
Function Purpose
char Convert to character array (string)
Following table provides the functions for determining the data type
of a variable −
Function Purpose
is Detect state
Example
x = 3
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
isnumeric(x)
x = 23.54
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
isnumeric(x)
x = [1 2 3]
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
x = 'Hello'
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
isnumeric(x)
x=3
ans = 0
ans = 1
ans = 1
ans = 1
ans = 1
x = 23.540
ans = 0
ans = 1
ans = 1
ans = 1
ans = 1
x=
1 2 3
ans = 0
ans = 1
ans = 1
ans = 0
x = Hello
ans = 0
ans = 0
ans = 1
ans = 0
ans = 0