L2-Getting Started With Matlab
L2-Getting Started With Matlab
with Matlab
MATLAB Environment
The MATLAB environment is command oriented somewhat like UNIX.
A prompt appears on the screen and a MATLAB statement can be
entered. When the <ENTER> key is pressed, the statement is
executed, and another prompt appears.
The MATLAB environment (on most computer systems) consists of
menus, buttons and a writing area similar to an ordinary word
processor.
Matlab Desktop
Command Window
Workspace /
Current Directory
Command
History
3
Matlab Workspace
The workspace is Matlab’s memory. Displays any variables created (Matrices, Vectors, Singles,
etc.
Can manipulate variables stored in the workspace
>> b=10;
>> c=a+b
c=
22
>>
If a statement is terminated with a semicolon ( ; ), no results will be displayed. Otherwise results
will appear before the next prompt.
Matlab Variable names
Variable names ARE case sensitive
Variable names can contain up to 63 characters (as of MATLAB 6.5 and newer)
Variable names must start with a letter followed by letters, digits, and
underscores.
Variable, function, file names
Rules
◦ is case sensitive, e.g., NAME and Name are 2 distinct names.
◦ variable begins with a letter, e.g., A2z, a_z or a2z
◦ can be a mix of letters, digits, and underscores (e.g., vector_A)
◦ reserved characters: % = + – ~ ; : ! ' [ ] ( ) , @ # $ &
^
◦ up to 63 characters
◦ A function performs a pre-defined task based on input to yield
certain outcome.
Variable, function, file names
File name
◦ MATLAB command files should be named with a suffix
of ".m", e.g., myfile.m. An m-file typically contains a
sequence of MATLAB commands that will be executed
in order
◦ A file may contain a collection of commands,
functions
Note: To run, enter m-file, without .m, e.g.,
>> myfile
Variables
Don’t have to declare type
Don’t even have to initialise
Just assign in command window Try the same line without
the semicolon and
>> comments
Matlab
prompt comment
suppress operator
assign
command
operator
output
8
Variable
View variable contents by simply typing the variable name at the command prompt
>> a
a=
12
>>
>> a*2
a=
24
>>
Deleting variables
Delete variable(s) from workspace
>> clear a b; % delete a and b from workspace
Reserved characters (% = ; ,)
Some characters are reserved by MATLAB for various purposes.
Some as arithmetic or matrix operators: =, +, - , *, / , \ and others
are used to perform a multitude of operations. Reserved characters
cannot be used in variable or function names.
>> % anything after % until the end of line is treated as comments
>>
>> a = 3 % define a to have the value 3
a=
3
Reserved characters % = ; ,
>> a = 3; % “;” suppresses printing
>>
>> b = 4; c = 5; % “;” enables multiple commands on same line
>>
>> d = 6, e = 7; % “,” delimits commands but enables printing
d=
6
Matlab help commands
help
>> help whos % displays documentation for the function whos
>> lookfor convert % displays functions with convert in the first help line
>> whos
Name Size Bytes Class
a 1x1 8 double array
b 1x1 8 double array
c 1x1 8 double array
Grand total is 3 elements using 24 bytes
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
MATLAB Math & Assignment
Operators
Power ^ or .^ a^b or a.^b
Multiplication * or .* a*b or a.*b
Division / or ./ a/b or a./b
or \ or .\ b\a or b.\a
NOTE: 56/8 = 8\56
- (unary) + (unary)
Addition + a + b
Subtraction - a - b
Assignment = a = b (assign b to a)
Other MATLAB symbols
>> prompt
... continue statement on next line
, separate statements and data
% start comment which ends at end of line
; (1) suppress output
(2) used as a row separator in a matrix
: specify range