Introduction To Matlab: Overview and Environment Setup
Introduction To Matlab: Overview and Environment Setup
Introduction To Matlab: Overview and Environment Setup
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,
>> 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
Operatocharacters −
Purpose Operator Purpose
r () Parentheses; encloses function
+ Plus; addition operator. arguments and array indices;
- Minus; subtraction operator. overrides precedence.
* Scalar and matrix multiplication [] Brackets; enclosures array
operator. elements.
.* Array multiplication operator. . Decimal point.
^ Scalar and matrix exponentiation … Ellipsis; line-continuation operator
operator. , Comma; separates statements and
.^ Array exponentiation operator. elements in a row
\ Left-division operator. ; Semicolon; separates columns and
suppresses display.
/ Right-division operator.
% Percent sign; designates a comment
.\ Array left-division operator.
and specifies formatting.
: Colon; generates regularly spaced
_ Quote sign and transpose operator.
elements and represents an entire
row or column. ._ Nonconjugated transpose operator.
= Assignment operator.
MATLAB – BASIC SYNTAX
Special Variables and Constants
MATLAB supports the following special variables and constants −
Name Meaning
ans Most recent answer.
eps Accuracy of floating-point precision.
i,j The imaginary unit √-1.
Inf Infinity.
NaN Undefined numerical result (not a
number).
pi The number π
MATLAB – BASIC SYNTAX
Naming Variables
MATLAB is case-sensitive.
MATLAB – BASIC SYNTAX
Saving Your Work
For example,
x=3
>> sqrt(78)
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
>> 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
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
>> 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 −
Comman Purpose
d
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.
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,
Comma etc. Purpose Comma Purpose
nd
The following table displays some commonly
nd used system-related
cd Changes current directory. path Displays search path.
commands −
date Displays current date. pwd Displays current directory.
delete Deletes a file. save Saves workspace variables
diary Switches on/off diary file in a file.
recording. type Displays contents of a file.
dir Lists all files in current what Lists all MATLAB files in the
directory. current directory.
load Loads workspace variables wklread Reads .wk1 spreadsheet file.
MATLAB – COMMANDS
Input and Output Commands
MATLAB provides the following input and output related commands −
Comman Purpose
d
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
MATLAB – COMMANDS
The fscanf and fprintf commands behave like C scanf and
printf functions. They support the following format codes −
Format Purpose
Code
%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.
%g Format in the most compact form: %f or
%e.
\n Insert a new line in the output string.
\t Insert a tab in the output string.
MATLAB – COMMANDS
The format function has the following forms used for
numeric display −
Format Display up to
Function
format short Four decimal digits (default).
format long 16 decimal digits.
format short e Five digits plus exponent.
format long e 16 digits plus exponents.
format bank Two decimal digits.
format + Positive, negative, or zero.
format rat Rational approximation.
format Suppresses some line feeds.
compact
format loose Resets to less compact display mode.
MATLAB – COMMANDS
Vector, Matrix and Array Commands
The following table shows various commands used for working with
arrays, matrices and vectors −
Comma Purpose
nd
det Computes determinant of an array.
inv Computes inverse of a matrix.
pinv Computes pseudoinverse of a matrix.
rank Computes rank of a matrix.
rref Computes reduced row echelon form.
cell Creates cell array.
celldisp Displays cell array.
cellplot Displays graphical representation of cell array.
num2cel Converts numeric array to cell array.
l
deal Matches input and output lists.
iscell Identifies cell array.
MATLAB – COMMANDS
Plotting Commands
MATLAB provides numerous commands for plotting graphs. The
following table shows some of the commonly used commands for
plotting −
Comma Purpose Comman Purpose
nd d
axis Sets axis limits. close Closes the current plot.
fplot Intelligent plotting of close all Closes all plots.
functions. figure Opens a new figure window.
grid Displays gridlines. gtext Enables label placement by mouse.
plot Generates xy plot. hold Freezes current plot.
print Prints plot or saves plot to a legend Legend placement by mouse.
file.
refresh Redraws current figure window.
title Puts text at top of plot.
set Specifies properties of objects such
xlabel Adds text label to x-axis. as axes.
ylabel Adds text label to y-axis. subplot Creates plots in subwindows.
axes Creates axes objects. text Places string in figure.
close Closes the current plot. bar Creates bar chart.
MATLAB – COMMANDS
Plotting Commands
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.