0% found this document useful (0 votes)
7 views

MATLAB Basic Commands

The document provides a comprehensive overview of basic MATLAB commands, including their usage and examples. It covers operations such as addition, subtraction, matrix manipulation, trigonometric functions, and plotting, along with notes on syntax and functionality. Additionally, it emphasizes the importance of using MATLAB's help feature for further information on commands.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

MATLAB Basic Commands

The document provides a comprehensive overview of basic MATLAB commands, including their usage and examples. It covers operations such as addition, subtraction, matrix manipulation, trigonometric functions, and plotting, along with notes on syntax and functionality. Additionally, it emphasizes the importance of using MATLAB's help feature for further information on commands.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

MATLAB Basic Commands

e.g Example
Command Usage Notes
# Command Result
+ Addition 3+4 7 In case of matrix addition or
1 [1 2 3]+[4 5 6] [5 7 9] subtraction, both matrices have to
- Subtraction 7-3 4 have same dimensions
 Unary Multiplication 2*4 8
 ( ) ( ) ( )
2 * [1 2 3] *[4  For more info about the brackets [],
 Matrix Multiplication 5 32 See example # 7
6]  Refer to Matrices multiplication for
more information
3 / Division 8/2 4 You can use the brackets ( ) as
4 ^ Power (exponent) 2^3 8 (2+3)^2/5
 Wherever you enter the variables
( a or b or My_var ) in your
code, they will represent their
a=10 a=10 values.
 Assign a value to a
5 = b=3+2 b=5  You can name the variable by any
variable My_var=a/b My_var=2 name, but be aware that variables
are case sensitive and have some
restrictions, so any mismatch will
cause an error.

y=sin(0) y=0
sin( )
6 trigonometric functions y2=sin(pi/2) y2=1
cos( )
y3=cos(0) y3=1
 The input argument has to be in
radian
 pi is a constant equal to
- Here we defined a matrix named
MyMatrix has 1 row and 3
7 [ ] Define a new matrix MyMatrix= [ 2 5 3] MyMatrix= [ 2 5 3] coulomns .
- You can use space to separate
between matrix’s element
Same as the previouse example but
8 , Define a new columns MyMatrix= [2,5,3] MyMatrix= [ 2 5 3]
here we used comma instead of space
you can use enter instead as
MyMatrix=[1,2,3;4, MyMatrix=[1 2 3
 Define a new row 5,6;7,8,9] MyMatrix= [ ]
4 5 6
7 8 9]
9 ;  Used at the end of
Nothing will appear
command line to It’s very useful to boost the speed of
in command window,
avoid presenting the A=[1 2]; your code’s execution as well as to
but the matrix A
outcome in command will be defined. avoid redundant outcomes
window
Create a vector of serial
10 : 1:5 [ ]
numbers

1:2:9 [ ]
Create a vector with
11 : :
certain increment step a=-0.6; b=0; c=0.2;
[ ]
a:c:b
It can be used after matrix’s name
Matrix transpose. i.e. [ 1 2 3]’ [ ] e.g. MyMatrix’
12 ‘ change the rows to be i.e. in all examples you can use the
columns and vice versa [1 2 ; 3 4]’ [ ] matrix’s name instead of matrix
itself
In arry multiplication each element
in the matrix is multiplied by the
13 .* Array Multiplication [1 2 3].*[ 3 4 5] [ ] corresponding one in the other
matrix, thus the diamensions both
matricies must agree.
14 .^ Array Power [1 2 3].^2 [ ] You can use also 2.^[2 3 4]
e.g Example
Command Usage Notes
# Command Result
A= [3 4 5 Return the element in row
Used after the matrix’s
7 3 4 number 3 and column number 2
15 ( , ) name to return a specific 1
2 1 5];
element in that matrix A(3,2) in the matrix A
x=[2 5 10 4];  Many useful ways to use max()
max( ) Find the maximum and
16 max(x) 10
min( ) minimum values and min(). See MATLAB help
min(x) 2
sum([ 1 5 3]) 9  It can be used an integral
17 sum( ) Gives the addition result  In case of matrix has more than
A=[1 2 3; 4 5 6 ]; one row, it adds each colum’s
sum (A) [ 5 7 9] elements separately.
 The condition A>4 i.e. all
A=[1 4 6]; elements in the matrix A greater
Fine elements that than 4.
satisfy a certain C1=find(A>4) C1=3
18 find( )  & means both condition have to
condition and return its A(C1) 6 be satisfied.
index C2=find(A>1 & A<6) C2=2
 Use MATLAB help to find more
A(C2) 4 info.
Note that each (%) will eleminate
% add any comment No execution will be carried anything on its right for one line
19 % Comment % or explanation out, any command or line only. i.e. if your comments are more
% for your code begin with % will be ignored than one line you have to add % for
each line as in this example.

 You can change the color of the


curve and the line’s width, many
Locate a point in the
x=[1 2 3]; properties can be found in
coordinates x and y on
20 Plot(x,y) y=[1 4 2]; MATLAB help
xy plane and connect Plot(x,y)  To add title and labels for the
then with a straight line
axes see example

It is useful to represent an impulse


response of a system

Same as plot but x=[1 2 3];


21 stem( ) without connecting the y=[1 4 2];
points with each other stem(x,y)

Type this command before the plot


To add subplots to a
command, because this command
Subplot figure. i.e. divide the
22 Subplot(2,1,1) just creates a new subplot, and
(m,n,p) figure plane into
make it as the current plot plane.
subplots
Learn more about it in MATLAB help

Defined a string (text) x=[0:0.1:2*pi];  All of these properties can be used


‘ ’ variable i.e. As letters not y=sin(x); in any figure either you used plot
23 numbers plot(x,y); or stem or any MATLAB plotting
Add text to depicts the commands.
legend(‘txt’) title('One Cycle
curve
Sinusoidal Wave')  To change the curve’s color of
xlabel(‘txt’) width, search for plot in MATLAB
24 ylabel(‘txt’) Add labels to x and y axes
legend('Sin(x)'); help
25 title(‘txt’) Add title to the current plot xlabel('Angel
(radian)')
26 grid Show the grid of the plot ylabel('Amplitude')
 In all above examples we can use the matrix’s name instead of the matrix itself.
 Whenever you want to know more about any symbol, operation, or function, type in the command window “help” followed by the symbol or the
name of the command that you want to know more about it. Alternatively, select the required word and then press “F1” to give you a quick help.
Also you can use “lookfor” in command to search for any MATLAB command.

Done By: Mohammad Tamim for Dr. Ali Muaibel

You might also like