Introduction to MATLAB - 01
Introduction to MATLAB - 01
Course Instructors
Dr. Susant Kumar Panigrahi Dr. Anirban Bhowmick
Assistant Professor (SEEE) Assistant Professor Sr. (SEEE)
PhD, NIT Rourkela PostDoc, IISc Bangalore
Area of Expertise: Inverse Problems in Image Area of Expertise: Speech and Audio
Processing, Sparse Signal Representation and Processing, Speech Enhancement, Speech
Analysis, Computer Vision, Embedded System Recognition, Language identification and
Design and Programming. Audio Event Detection
Email: [email protected] Email: [email protected]
Pre-Requisite
3
Students are requested to create their respective accounts in MATLAB for online coding
and simulation:
https://in.mathworks.com/products/matlab-online.html
Click here to create your
own account
11
Structures
Brief History
The name MATLAB stands for MATrix LABoratory.
MATLAB Help
View or Change Current Folder or Dictionary
Command Window
Run single line of code and verify
Details
About the selected item from
the current folder.
Bridge Course Organized By: School of Electrical and Electronics Engineering
Variables Getting Started with MATLAB. 14
Variable names must start with a letter followed by letters, digits, and underscores( Valid:
x1 = 5, Invalid: 1x = 5)
Variable names are case sensitive (a1 does NOT equal A1)
You will have noticed that if you do not specify an output variable, MATLAB uses a default
variable ans, short for answer, to store the results of the current calculation.
Q2. Find the variable type of ‘ans’. Do the same operation by changing variable type to unit8 and assign a
variable name X to hold the answer. Compare the memory size of ‘ans’ and X.
Using MATLAB as a Calculator
Getting Started with MATLAB. 18
a) 6 x 4 ÷ 12 + 72 ÷ 8 – 9
For example, in the following, we left out the multiplication sign, *, in the following expression
There are two main types of errors that can be introduced while
programming in any language:
1. Syntax Errors
2. Algorithmic Errors
1. Syntax Errors:
A syntax error occurs when the calling syntax you use for a function is incorrect, or when you
provide the function with inputs that are of the wrong shape, size, and/or type, or are otherwise not valid
for the function in question. A typo, or typographic error, could be considered as a syntax error.
Bridge Course Organized By: School of Electrical and Electronics Engineering
Error messages
Getting Started with MATLAB. 21
Assignment Error
Parenthesis Error
Miscellaneous Error
Other Arithmetic Errors: Will very often result in errors or useless results unless measures
are taken to avoid propagating them.
MATLAB® displays output in both the Command Window and the Live Editor.
Style Result Example
Examples:
short Short, fixed-decimal format with 4 digits after 3.1416
the decimal point. This is the default
numeric setting.
long Long, fixed-decimal format with 15 digits 3.141592653589793
after the decimal point for double values, and
7 digits after the decimal point
for single values.
shortE Short scientific notation with 4 digits after the 3.1416e+00
decimal point.
longE Long scientific notation with 15 digits after 3.141592653589793e+00
the decimal point for double values, and 7
digits after the decimal point for single values.
shortG Short, fixed-decimal format or scientific 3.1416
notation, whichever is more compact, with a
total of 5 digits.
longG Long, fixed-decimal format or scientific 3.14159265358979
notation, whichever is more compact, with a
total of 15 digits for double values, and 7
digits for single values. Bridge Course Organized By: School of Electrical and Electronics Engineering
Getting Started with MATLAB. 24
Examples:
Returns a string. Here variable will be ‘s’ which is assign to the sting.
Example -- s = whos (): returns structure array with fields: name, size, bytes, class, global,
sparse, complex, nesting, persistent.
Examples:
Create a string array and compute its length, which is the number of
elements in each row.
save(filename) – stores all variables from the current workspace in a MATLAB formatted binary file (MAT-file)
called filename.
The linspace function generates linearly spaced vectors. It is similar to the colon operator ":", but gives direct
control over the number of points.
y = linspace(a,b) generates a row vector y of 100 points linearly spaced between a and b.
y = linspace(a,b,n) generates n points.
ans =
1.0000 3.2500 5.5000 7.7500 10.0000
format etc…
Sets format for output to display.
Input: format short
Pi
Output: 3.1416
Functions/Co
Most Commonly used MATALAB Syntax Definition Example
mmands
ones(n): Returns n x n matrix with
each element of value 1.
X = ones(n) ‘or’
ones ones(n,m):returns n x m matrix
Y = ones(m,n) with each element of value 1.
Commands: At a Glance
Input: X = 3+j*4
Returns absolute value and complex
abs Y = abs(X) Y = abs(X)
magnitude.
Output: Y = 5.
Input: X = 3+j*4
Commands: At a Glance