logo for matrixlab-examples.com
leftimage for matrixlab-examples.com

More MATLAB Commands...

On this page we present the use of some very basic MATLAB commands and functions. With Matlab, you can perform simple operations, as if you were working with a calculator.

Operation Symbol Example
Addition, a+b + 5 + 3
Subtraction, a-b - 20 - 9
Multiplication, ab * 8.7 * 9.2
Division, a/b / 40 / 8
Power, ab ^ 5^3

Common Functions

abs(x) absolute value
acos(x) inverse cosine, result in radians
angle(x) angle of complex number
asin(x) inverse sine, result in radians
atan(x) inverse tangent
ceil(x) round towards plus infinity
conj(x) complex conjugate
cos(x) cosine
exp(x) exponential
fix(x) round towards zero
floor(x) round towards minus infinity
imag(x) complex imaginary part
log(x) natural logarithm
log10(x) common (base 10) logarithm
real(x) complex real part
round(x) round towards nearest integer
sign(x) signum function
sin(x) sine
sqrt(x) square root
tan(x) tangent

Matlab doesn't care about spaces between operands, and multiplications are performed before additions. Type these operations on the command window (only the part just after '>>', and Matlab will give the answers):
>> 2  +  1  +  3
ans =

>> 8*9  -  6*5  +  3*4
ans =

>> x=2
x =

>> y=3
y =

>> cos(x)+sqrt(y)+tan(x+y)
ans =

You can keep values in variables:

>> apples=2
apples =

>> bananas=5
bananas =

>> peaches=4
peaches =

>> fruit=apples+bananas+peaches
fruit =

Ending the instruction with a ';' sign tells Matlab to make the operation but not to display the answer.

To see a list of your variables in memory you can use the commands 'who' or 'whos'.

>> whos
Name Size Bytes Class
ans 1x1 8 double array
apples 1x1 8 double array
bananas 1x1 8 double array
fruit 1x1 8 double array
peaches 1x1 8 double array

Grand total is 5 elements using 40 bytes

Variables are sensitive to lower/upper case. They have to start with a letter and can contain also numbers and underscores.

Special Names Value
ans Default for results
pi 3.141592...
eps Smallest possible number in Matlab
inf Infinite
NaN Not a number
i square root of -1
j square root of -1

To delete variables you can use the 'clear' command.

Operations with Complex Numbers

>> c1 = 1 - 2i
c1 =
   1.0000 - 2.0000i

>> c2 = 6 - 9j
c2 =
   6.0000 - 9.0000i

>> c3 = sqrt(-2)
c3 =
        0 + 1.4142i

>> c4 = c1 + c2 - c3
c4 =
   7.0000 -12.4142i

Matlab allows you to place your commands on a simple text file. You should name this file with the '.m' extension. Giving Matlab the name of this file while in the command window, makes Matlab to execute the script.

For example, name a text file 'primer.m'.
Make it contain the following text:

a = 3; b = -4; c = 8;
d = a*b-c

When you execute this from the command window

>> primer

you obtain this information from MATLAB:

d  =


M-File functions
disp(ans) show results without showing the variable name
input prompt for user input
keyboard stops execution of the m-file and gives control to the
user's keyboard ('return' to quit)
pause causes a procedure to stop and wait for the user to
strike any key before continuing
pause(n) pauses for n seconds
waitforbuttonpress wait for key/buttonpress over figure

 From 'Matlab Commands' to home   

 From 'Matlab Commands' to Matlab Help Menu


Vector basics

Video: Getting started

footer for matlab commands page