image thumbnail

"Getting Started with MATLAB" Video: Commands and Script

by

 

27 Apr 2009 (Updated )

Commands and script used in "Getting Started with MATLABĀ®" Video on MATLAB demo page.

Getting Started with MATLAB

Getting Started with MATLAB

Welcome to this MATLAB Video tutorial. If you have never used MATLAB before, this demonstration will get you started and show you where to go to next to learn more.

Contents

Creating Variables

The MATLAB language lets you construct commands to create and process variables. You can create variables by entering them in the command window here. For example,

a = 1
a =

     1

b = 2
b =

     2

c = a+b
c =

     3

or...

d = cos(a)
d =

    0.5403

Creating Vectors

MATLAB is an array based language where variables can be vectors, matrices or N dimensional arrays. You use square brackets to construct arrays. To create a row vector you can type,

t=[1 2 3 4 5]
t =

     1     2     3     4     5

You can use the colon operator to simplify the creation of equally spaced arrays.

t = 1:5 % t equals 1 to 5
t =

     1     2     3     4     5

You can recall previously entered commands by dragging them from the command history here or by pressing the up-arrow key. You can then edit it...

t=0:.01:1 % t goes from 0 in steps of .01 to 1
t =

  Columns 1 through 6

         0    0.0100    0.0200    0.0300    0.0400    0.0500

  Columns 7 through 12

    0.0600    0.0700    0.0800    0.0900    0.1000    0.1100

  Columns 13 through 18

    0.1200    0.1300    0.1400    0.1500    0.1600    0.1700

  Columns 19 through 24

    0.1800    0.1900    0.2000    0.2100    0.2200    0.2300

  Columns 25 through 30

    0.2400    0.2500    0.2600    0.2700    0.2800    0.2900

  Columns 31 through 36

    0.3000    0.3100    0.3200    0.3300    0.3400    0.3500

  Columns 37 through 42

    0.3600    0.3700    0.3800    0.3900    0.4000    0.4100

  Columns 43 through 48

    0.4200    0.4300    0.4400    0.4500    0.4600    0.4700

  Columns 49 through 54

    0.4800    0.4900    0.5000    0.5100    0.5200    0.5300

  Columns 55 through 60

    0.5400    0.5500    0.5600    0.5700    0.5800    0.5900

  Columns 61 through 66

    0.6000    0.6100    0.6200    0.6300    0.6400    0.6500

  Columns 67 through 72

    0.6600    0.6700    0.6800    0.6900    0.7000    0.7100

  Columns 73 through 78

    0.7200    0.7300    0.7400    0.7500    0.7600    0.7700

  Columns 79 through 84

    0.7800    0.7900    0.8000    0.8100    0.8200    0.8300

  Columns 85 through 90

    0.8400    0.8500    0.8600    0.8700    0.8800    0.8900

  Columns 91 through 96

    0.9000    0.9100    0.9200    0.9300    0.9400    0.9500

  Columns 97 through 101

    0.9600    0.9700    0.9800    0.9900    1.0000

Adding a semicolon avoids command output being echoed to the command window.

t=0:.01:1;

The whos Command and WSB

To see what variables you have created so far type,

whos
  Name      Size             Bytes  Class     Attributes

  a         1x1                  8  double              
  b         1x1                  8  double              
  c         1x1                  8  double              
  d         1x1                  8  double              
  t         1x101              808  double              

...or view a list in the workspace browser here

To see the value of a variable just type its name such as,

b
b =

     2

Vector Operations

You carry-out operations on vectors just like simple scalars. For example,

y = sin(2*pi*t)
y =

  Columns 1 through 6

         0    0.0628    0.1253    0.1874    0.2487    0.3090

  Columns 7 through 12

    0.3681    0.4258    0.4818    0.5358    0.5878    0.6374

  Columns 13 through 18

    0.6845    0.7290    0.7705    0.8090    0.8443    0.8763

  Columns 19 through 24

    0.9048    0.9298    0.9511    0.9686    0.9823    0.9921

  Columns 25 through 30

    0.9980    1.0000    0.9980    0.9921    0.9823    0.9686

  Columns 31 through 36

    0.9511    0.9298    0.9048    0.8763    0.8443    0.8090

  Columns 37 through 42

    0.7705    0.7290    0.6845    0.6374    0.5878    0.5358

  Columns 43 through 48

    0.4818    0.4258    0.3681    0.3090    0.2487    0.1874

  Columns 49 through 54

    0.1253    0.0628    0.0000   -0.0628   -0.1253   -0.1874

  Columns 55 through 60

   -0.2487   -0.3090   -0.3681   -0.4258   -0.4818   -0.5358

  Columns 61 through 66

   -0.5878   -0.6374   -0.6845   -0.7290   -0.7705   -0.8090

  Columns 67 through 72

   -0.8443   -0.8763   -0.9048   -0.9298   -0.9511   -0.9686

  Columns 73 through 78

   -0.9823   -0.9921   -0.9980   -1.0000   -0.9980   -0.9921

  Columns 79 through 84

   -0.9823   -0.9686   -0.9511   -0.9298   -0.9048   -0.8763

  Columns 85 through 90

   -0.8443   -0.8090   -0.7705   -0.7290   -0.6845   -0.6374

  Columns 91 through 96

   -0.5878   -0.5358   -0.4818   -0.4258   -0.3681   -0.3090

  Columns 97 through 101

   -0.2487   -0.1874   -0.1253   -0.0628   -0.0000

This makes use of the constant pi, pre-defined in MATLAB.

Basic Plotting

You can plot y against t with...

plot(t,y) % the plot command.

Complex Numbers

In MATLAB, variables can be complex; with i used to denote the imaginary part such as...

x= 3 + 4i
x =

   3.0000 + 4.0000i

Creating Matrices

You enter matrices using the semicolon in the following way,

a = [1 2 3; 4 5 6; 7 8 10]
a =

     1     2     3
     4     5     6
     7     8    10

...or you can use functions.

Function Browser and Hints

You can browse a list of available functions in MATLAB by clicking this icon, and browsing functions by category or by searching using keywords here. Here we will generate a matrix of random numbers. Double clicking enters the function name.

Pausing after typing a parentheses shows a list of possible arguments

data=rand(5,5)
data =

    0.7577    0.7060    0.8235    0.4387    0.4898
    0.7431    0.0318    0.6948    0.3816    0.4456
    0.3922    0.2769    0.3171    0.7655    0.6463
    0.6555    0.0462    0.9502    0.7952    0.7094
    0.1712    0.0971    0.0344    0.1869    0.7547

Help

You can access help on all of MATLAB by clicking on the question mark here, then browse or search for information.

Accessing Demonstrations

You can access demonstrations and getting started documentation from this message bar.

You can find the dimensions of an array with the size function.

size(data)
ans =

     5     5

...which is also shown in the workspace browser.

Matrix Operations

You can perform matrix operations such as...

b = a' % b = the transpose of a.
b =

     1     4     7
     2     5     8
     3     6    10

c = a*b % c = a times b, which performs matrix multiplication,...
c =

    14    32    53
    32    77   128
    53   128   213

...or

c = a.*b %  c = a dot times b
c =

     1     8    21
     8    25    48
    21    48   100

...which performs element-wise multiplication, where the corresponding elements of each matrix are multiplied.

You could calculate the inverse of matrix a...

inv(a)
ans =

   -0.6667   -1.3333    1.0000
   -0.6667    3.6667   -2.0000
    1.0000   -2.0000    1.0000

inv(a)*a % and multiply this by a...
ans =

    1.0000         0    0.0000
         0    1.0000         0
   -0.0000   -0.0000    1.0000

...to confirm you get the identity matrix.

Indexing

You can select elements or sections of an array by indexing. For the variable a,

a
a =

     1     2     3
     4     5     6
     7     8    10

Here is the value at...

a(2,3) % ...the second row and third column
ans =

     6

Or for the variable data,

data
data =

    0.7577    0.7060    0.8235    0.4387    0.4898
    0.7431    0.0318    0.6948    0.3816    0.4456
    0.3922    0.2769    0.3171    0.7655    0.6463
    0.6555    0.0462    0.9502    0.7952    0.7094
    0.1712    0.0971    0.0344    0.1869    0.7547

...here is the section from,

data(1:3,2:end) % rows 1 to 3 and columns 2 to the end.
ans =

    0.7060    0.8235    0.4387    0.4898
    0.0318    0.6948    0.3816    0.4456
    0.2769    0.3171    0.7655    0.6463

You can set values in this way too. For example, with data, you could

data(1:2, :) = 0 % set rows 1:2 and all the columns to zero.
data =

         0         0         0         0         0
         0         0         0         0         0
    0.3922    0.2769    0.3171    0.7655    0.6463
    0.6555    0.0462    0.9502    0.7952    0.7094
    0.1712    0.0971    0.0344    0.1869    0.7547

The colon operator used on its own in indexing, specifies "all elements", in this case, all columns.

Note that array indices in MATLAB start at 1.

Plotting Matrices

And you can plot matrices as well. If you wanted to display the matrix w...

w=y'*y; % ...generated by multiplying the transpose of the sine wave vector y, with itself...

...you could enter...

surf(w);

...which creates a surface plot.

Conclusion

That concludes the demonstration. You can try some of these examples in MATLAB now or watch one of the other videos.

Contact us