Bushra Fatima NED University of Engineering & Technology: April 11, 2017
Bushra Fatima NED University of Engineering & Technology: April 11, 2017
INTRODUCTION TO MATLAB
Bushra Fatima
NED University of Engineering & Technology
April 11, 2017
Outline
• Default view of MATLAB desktop
• Command Window
• Command History.
Textbook: A. Gilat, Matlab: An Introduction with Applications 4th Edition. John Wiley
& Sons, Incorporated, 2017. (Chapter 1: Introduction)
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 3 of 38
Introduction
• MAT/LAB: Matrix Laboratory
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 4 of 38
• Mathematics
• An algorithm
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 5 of 38
CURRENT WORKSPACE
FOLDER WINDOW
WINDOW
COMMAND WINDOW
COMMAND HISTORY
WINDOW
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 6 of 38
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 7 of 38
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 8 of 38
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 9 of 38
• Editing:
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 10 of 38
Command-line editing:
• Left-arrow, Right-arrow
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 11 of 38
Comment
Comment line is not executed when ENTER is
pressed
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 14 of 38
• It is not interactive.
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 17 of 38
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 18 of 38
• whos Lists all the current variables in more detail than who.
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 19 of 38
• clc clears the command window (but does not clear the saved
variables in the workspace)
• clear all deletes all the saved variables from the workspace (but
does not clear the command window) and brings the cursor at top
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 20 of 38
Busy…
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 21 of 38
Command History
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 22 of 38
Addition: 3+2
Subtraction: 3-2
Multiplication: 3*2
Division: 3/2 or 2\3
Exponent: 3^2
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 23 of 38
Order of Precedence
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 24 of 38
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 25 of 38
Display formats
Command Description Example
>> 290/7
format short
upto 4 decimal digits ans =
(default)
41.4286
>> 290/7
ans =
format long upto 15 decimal digits
41.4285714285
71431
>> 290/7
format short e scientific notation with 4 decimal digits ans =
4.1429e+001
>> 290/7
ans =
format long e scientific notation with 15 decimal digits
4.14285714285
7143e+001
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 26 of 38
Display formats
Command Description Example
>> 290/7
format short g 5 s.g.f of floating digits ans =
41.429
>> 290/7
ans =
format long g 15 s.g.f of floating digits
41.4285714285
714
>> 290/7
format bank upto 2 decimal digits ans =
41.43
Eliminates empty lines to allow more lines with information
format compact
displayed on the screen.
format loose
Adds empty lines (opposite of compact).
(default) METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 27 of 38
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 28 of 38
Infinity:
1/0
inf + 3
Not-a-Number (NaN):
0/0
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 29 of 38
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 30 of 38
break for
case function end
catch global return
classdef if spmd
continue otherwise switch
else parfor try
elseif persistent while
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 32 of 38
ans
pi
…
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 33 of 38
a= 4
a = sqrt(4)
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 34 of 38
Practice …..
feet = fix(40/12)
inches = rem(40, 12)
METU NCC