You are on page 1of 17

Basics of MATLAB

Lecture 2
Eng. Mohamed Awni
Electrical & Computer Engineering
Dept.

Agenda
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

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

lists all of the variables in your matlab

>>Whos

list the variables and describes their matrix size

Matlab data type


Variable
Value

Size

Class

Matlab data type


Multidimensional Array
[full or sparse]

logical

char

ab

int8,
int16,
int32,
int64,

numeric

uint8, single
unit16,
unit32,
unit64

cell

{ }
double

struct

Arithmetic Operator & Their Precedence


Operations

Addition
Subtraction
Multiplication
Right Division
Left Division
Exponentiation
Precedence Order

1
2
3
4

Operators

*
/
\
^

Examples

>> +
>>
>>
>> /
>> \ = /
>> ^ (means = )

Operators

Parentheses ( ). For nested parentheses, the innermost


are executed first.
Exponentiation, ^
Multiplication, *; Division, /,\
Addition, +; Subtraction, -

Examples

The use of the comma (,) and the semicolon (;)

Variables
No need for types. i.e.,
int a;
double b;
float c;

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.
>>x=6;
>>y=7;

All variables are created with double precision unless specified


and they are matrices.
10

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.

Variables assignment
Using the assignment operator =
Variable_name = A numerical value, or a computable expression

Elementary math built-in functions


MATLAB program that performs a sequence
of operations specified in a text file called an
m-file
A function should have a name

Matlab Functions 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

You might also like