Lecture 2
Basics of MATLAB
Eng. Mohamed Awni
Electrical & Computer Engineering
Dept.
Agenda
2
Useful Commands used frequently.
Matlab data type?
Arithmetic Operator & Their Precedence
Examples
The Use of (,) and (;) in matlab
Variables
Elementary math built-in functions.
Examples & Exercise & Homework
3
Useful Commands
β’ There are some commands used most by Matlab users are
>>help functionname
>>lookfor keyword
Useful Commands
>>clc clears the Command Window.
>>clear removes all variables from the workspace.
>>Who
>>Whos
lists all of the variables in your matlab
list the variables and describes their matrix size
5
Matlab data type
Class
Variable
Size Value
Multidimensional Array [full or sparse]
logical
ab
char
{ } cell struct numeric
int8, uint8, int16, unit16, int32, unit32, int64, unit64
single double
Matlab data type
7
Arithmetic Operator & Their Precedence
Operations Operators Examples
Addition Subtraction Multiplication Right Division Left Division Exponentiation
+ β * / \ ^
>> π + π >> π β π >> π β π >> π/π >> π\π = π/π >> π^π (means ππ = πππ)
Precedence Order Operators
1 2 3 4
Parentheses ( ). For nested parentheses, the innermost are executed first. Exponentiation, ^ Multiplication, *; Division, /,\ Addition, +; Subtraction, -
8
Examples
9
The use of the comma (,) and the semicolon (;)
10
Variables
β’ No need for types. i.e.,
int a; double b; float c;
>>x=6; >>y=7;
β’ All variables are created with double precision unless specified and they are matrices.
β’ When MATLAB encounters a new variable name, it automatically creates the variable and allocates the appropriate amount of memory storage.
β’ If the variable already exists, MATLAB changes its contents.
How to choose a variable name β’ Use meaningful names for variables
β’ MATLAB variable names
β must begin with a letter
β can contain any combination of letters, numbers and underscore (_)
- The variables name should not contain spaces.
-The maximum number of characters of a variables name in MATLAB R2009a is 63 characters.
β’ MATLAB is case sensitive: βnameβ, βNameβ and βNAMEβ are considered different variables.
β’ Never use a variable with the same name as a MATLAB command.
β’ Using the assignment operator =
Variables assignment
Variable_name = A numerical value, or a computable expression
Elementary math built-in functions
Matlab Functions
β’ MATLAB program that performs a sequence of operations specified in a text file called an m-file
β’ A function should have a name
β’ accepts one or more MATLAB variables (called arguments) between parenthesis as inputs
β’ operates on them in some way.
β’ returns one or more MATLAB variables as
outputs
Elementary math built-in functions
Examples
Calculate : y= 2*sqrt(a^2-c^2), where a=5 and c= 4
Exercises
Homework