1 Introduction To MATLAB
1 Introduction To MATLAB
• Overview
• The Advantages of MATLAB
• Disadvantages of MATLAB
• The MATLAB Environment
• Basic Syntax
• Variables
• Commands
MATLAB OVERVIEW
1. Ease of Use
2. Platform Independence
3. Predefined Functions
4. Device-Independent Plotting
5. Graphical User Interface
6. MATLAB Compiler
DISADVANTAGES OF MATLAB
1. It is an interpreted language
2. Expensive
THE MATLAB ENVIRONMENT
MATLAB development IDE can be launched from the icon created on the desktop.
The main working window in MATLAB is called the desktop. When MATLAB is
started, the desktop appears in its default layout −
THE MATLAB ENVIRONMENT
• Current Folder − This panel allows you to access the project folders and files.
THE MATLAB ENVIRONMENT
• Command Window − This is the main area where commands can be entered at
the command line. It is indicated by the command prompt (>>).
THE MATLAB ENVIRONMENT
• Workspace − The workspace shows all the variables created and/or imported
from files.
THE MATLAB ENVIRONMENT
• Command History − This panel shows or return commands that are entered at
the command line.
MATLAB – BASIC SYNTAX
MATLAB environment behaves like a super-complex calculator. You can enter
commands at the >> command prompt.
MATLAB is an interpreted environment. In other words, you give a command and
MATLAB executes it right away.
Hand on Practice
Another example,
Another example,
MATLAB provides some special expressions for some mathematical symbols like pi
for 𝜋𝜋, Inf for ∞, I (and j) for √-1 etc. Nan stands for ‘not a number’.
MATLAB – BASIC SYNTAX
Use Semicolon (;) in MATLAB
Semicolon (;) indicates end of statement. However, if you want to suppress and hide
the MATLAB output for an expression, add a semicolon after the expression.
For example,
>> x = 3;
>> y = x + 5
y=8
MATLAB – BASIC SYNTAX
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 % }
MATLAB – BASIC SYNTAX
Commonly Used Operators and Special Characters
MATLAB supports the following commonly used operators and special characters −
Operator Purpose Operator Purpose
+ Plus; addition operator. () Parentheses; encloses function arguments and
array indices; overrides precedence.
- Minus; subtraction operator.
[] Brackets; enclosures array elements.
* Scalar and matrix multiplication operator. . Decimal point.
.* Array multiplication operator. … Ellipsis; line-continuation operator
, Comma; separates statements and elements in
^ Scalar and matrix exponentiation operator. a row
.^ Array exponentiation operator. ; Semicolon; separates columns and suppresses
display.
\ Left-division operator.
% Percent sign; designates a comment and
/ Right-division operator. specifies formatting.
_ Quote sign and transpose operator.
.\ Array left-division operator.
._ Nonconjugated transpose operator.
: Colon; generates regularly spaced = Assignment operator.
elements and represents an entire row or
column.
MATLAB – BASIC SYNTAX
Special Variables and Constants
MATLAB supports the following special variables and constants −
Name Meaning
Inf Infinity.
pi The number π
MATLAB – BASIC SYNTAX
Naming Variables
Variable names can be of any length, however, MATLAB uses only first N characters,
where N is given by the function namelengthmax.
MATLAB is case-sensitive.
MATLAB – BASIC SYNTAX
Saving Your Work
The save command is used for saving all the variables in the workspace,
as a file with .mat extension, in the current directory.
For example,
You can reload the file anytime later using the load command.
MATLAB will execute the above statement and return the following result −
x=3
It creates a 1-by-1 matrix named x and stores the value 3 in its element.
MATLAB – VARIABLES
Let us check another example,
>> x = sqrt(16) % defining x and initializing it with an expression
MATLAB will execute the above statement and return the following result −
x=4
>> sqrt(78)
MATLAB will execute the above statement and return the following result −
ans = 8.8318
MATLAB – VARIABLES
You can use this variable ans −
>> sqrt(78);
>> 9876 / ans
MATLAB will execute the above statement and return the following result −
ans = 1118.2
You can have multiple assignments on the same line. For example,
>> a = 2; b = 7; c = a * b
MATLAB will execute the above statement and return the following result −
c = 14
MATLAB – VARIABLES
The who command displays all the variable names you have used.
>> who
Your variables are:
a ans b c
MATLAB – VARIABLES
The whos command displays all the variable names you have used.
• Variables currently in memory
• Type of each variables
• Memory allocated to each variable
• Whether they are complex variables or not
>> whos
MATLAB will execute the above statement and return the following result -
MATLAB will execute the above statement and return the following result -
final_velocity = 196
MATLAB – VARIABLES
The format Command
By default, MATLAB displays numbers with four decimal place values. This
is known as short format.
However, if you want more precision, you need to use the format command.
The format long command displays 16 digits after decimal.
For example −
The format bank command rounds numbers to two decimal places. For
example,
>> format bank
>> daily_wage = 177.45; weekly_wage = daily_wage * 6
weekly_wage = 1064.70
MATLAB – VARIABLES
MATLAB displays large numbers using exponential notation.
The format short e command allows displaying in exponential form with
four decimal places plus the exponent.
For example,
>> format short e
>> 4.678 * 4.9
ans = 2.2922e+01
The format long e allows displaying in exponential form with fourteen
decimal places plus the exponent. For example,
>> format long e
>> x = pi
x = 3.14159265358979e+80
MATLAB – VARIABLES
The format rat command gives the closest rational expression resulting from
a calculation. For example,
>> format rat
>> 4.678 * 4.9
ans = 2063 / 90
MATLAB – VARIABLES
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]
r= 7 8 9 10 11
MATLAB – VARIABLES
Another example,
>> r = [7 8 9 10 11];
>> t = [2, 3, 4, 5, 6];
>> res = r + t
res = 9 11 13 15 17
Column vectors are created by enclosing the set of elements in square brackets, using
semicolon(;) to delimit the elements.
>> c = [7; 8; 9; 10; 11]
c= 7
8
9
10
11
MATLAB – VARIABLES
Creating Matrices
A matrix is a two-dimensional array of numbers.
In MATLAB, a matrix is created by entering each row as a sequence of space or comma
separated elements, and end of a row is demarcated by a semicolon. For example, let
us create a 3-by-3 matrix as −
>> m = [1 2 3; 4 5 6; 7 8 9]
c= 1 2 3
4 5 6
7 8 9
MATLAB – COMMANDS
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.
MATLAB provides various commands for managing a session. The following table
provides all such commands −
Command Purpose
clc Clears command window.
clear Removes variables from memory.
exist Checks for existence of file or variable.
global Declares variables to be global.
help Searches for a help topic.
lookfor Searches help entries for a keyword.
quit Stops MATLAB.
who Lists current variables.
whos Lists current variables (long display).
MATLAB – COMMANDS
Commands for Working with the System
MATLAB provides various useful commands for working with the system, like saving the
current work in the workspace as a file and loading the file later.
It also provides various commands for other system-related activities like, displaying
date, listing files in the directory, displaying current directory, etc.
The following table displays some commonly used system-related commands −
Command Purpose Command Purpose
cd Changes current directory. path Displays search path.
date Displays current date. pwd Displays current directory.
delete Deletes a file. save Saves workspace variables in a file.
diary Switches on/off diary file recording. type Displays contents of a file.
dir Lists all files in current directory. what Lists all MATLAB files in the current
directory.
load Loads workspace variables from a
file. wklread Reads .wk1 spreadsheet file.
MATLAB – COMMANDS
Input and Output Commands
MATLAB provides the following input and output related commands −
Command Purpose
disp Displays contents of an array or string.
fscanf Read formatted data from a file.
format Controls screen-display format.
fprintf Performs formatted writes to screen or
file.
input Displays prompts and waits for input.
; Suppresses screen printing.
MATLAB – COMMANDS
The fscanf and fprintf commands behave like C scanf and printf functions.
They support the following format codes −
Format Code Purpose
%s Format as a string.
%d Format as an integer.
%f Format as a floating point value.
%e Format as a floating point value in scientific notation.
Command Purpose
Command Purpose
loglog Creates log-log plot.
polar Creates polar plot.
semilogx Creates semilog plot. (logarithmic abscissa).
semilogy Creates semilog plot. (logarithmic ordinate).
stairs Creates stairs plot.
stem Creates stem plot.