Matlab Sck Ppt
Matlab Sck Ppt
edit filename.m
(eg. edit script_demo.m)
or click
Operators (arithmetic)
+addition
.* element-by
element-by--
- subtraction element mult
* multiplication ./ element-
element-by
by--
/ division element div
^power .^ element-
element-by
by--
element power
‘ transpose
Operators (Relational, logical)
== equal
~= not equal
< less than
<= less than or equal
> greater than
>= greater than or equal
& AND
| OR
~ NOT
Output Format
format short 31.4159
format short e 3.1416e+001
format long 31.41592653589793
format long e 3.141592653589793e+001
format short g 31.416
format long g 31.4159265358979
format hex 403f6a7a2955385e
format rat 3550/113
format bank 31.42
Note: The default format is short
The format type is set by typing format type on the
command line.
Variables
Variable names must begin with a letter,
followed by any combination of letters,
numbers and the underscore (_) character.
character.
The MATLAB language is Case Sensitive.
NAME, name and Name are all different
variables..
variables
Never define a variable with the same
name as a MATLAB function or command.
MATLAB Special Variables
ans Default variable name for results
pi Value of
eps Smallest incremental number
inf Infinity
NaN Not a number e.g. 0/0
i and j i = j = square root of -1
realmin The smallest usable positive real
number
realmax The largest usable positive real
number
Arrays
The fundamental unit of data in MATLAB
Scalars are also treated as arrays by
MATLAB (1 row and 1 column).
column).
Row and column indices of an array start
from 1.
Arrays can be classified as vectors and
matrices..
matrices
Arrays (contd.)
Vector: Array with one dimension
e.g. if I == J
A(I,J) = 2;
elseif abs(I-
abs(I-J) == 1
A(I,J) = -1;
else
A(I,J) = 0;
end
Logic Control
(Switch Case and Otherwise)
More efficient than elseif statements
Only the first matching case is executed
color = input(‘color =’,’s’);
switch color
case ‘red’
c=[1 0 0]
case ‘green’
c=[0 1 0]
case ‘blue’
c=[0 0 1];
otherwise
disp (‘invalid choice of color’);
end
Iterative loops (For loop)
Similar to other N=10;
programming for I = 1:N
languages for J = 1:N
statement1
statement2
Repeats loop a set
number of times statement3
(based on index)
end
end
Can be nested
Iterative loops (while loop)
Similar to other % let us find all powers of 2
programming below 1000
languages v=[ ]; num=1; i=1;
while num < 1000
i=i+1;
Repeats loop until num=2^i;
logical condition v=[v num]
returns FALSE. end
v % display all powers of 2
below 1000
Can be nested.
Break, error, return
The command break inside a for or while loop
terminates the loop, even if the condition for execution is
true.
If the loops are nested then break terminates only the
innermost loop.
The command error(‘message’) inside a function or
script aborts the execution, displays the error message
and returns the control to keyboard.
The command return simply returns the control to
invoking function.
Function Files
A function file is also an m-
m-file except that variables
are local.
These are like that of functions in C.
A function file begins with a function definition line.
function [ output variables]=function_name(input
variables);
The function name must be the same as the file
name (without .m extension)
The first word in the function definition line, function,
must be a lowercase. A common mistake is to type
as Function.