MATLAB Examples

Section 6 - Creating Variables

You can create a PDF or HTML version of this MATLAB file by choosing opening it in the MATLAB Editor and choosing "File > Publish ..."

Contents

Introduction

4 + 10
ans =

    14

5 *10 + 6
ans =

    56

(6 + 6) / 3
ans =

     4

The ans Variable

4 + 5
ans =

     9

ans
ans =

     9

ans + 10
ans =

    19

Defining Your Own Variables

a = 10
a =

    10

b = 20
b =

    20

c = 30
c =

    30

a
a =

    10

the_average = (a + b + c) / 3
the_average =

    20

Listing Currently Defined Variables and Clearing Variables

clear % clear variables from memory
a = 5
a =

     5

b = 6
b =

     6

whos
  Name      Size            Bytes  Class     Attributes

  a         1x1                 8  double              
  b         1x1                 8  double              

clear
whos

Suppressing results with semicolons

a = 10;
b = 20;
c = 30;
d = 40;
the_average = (a + b + c + d) / 4
the_average =

    25

the_average;
b
b =

    20

e = 50
e =

    50

the_blank_average = (a + b + c + d + e) / 5;
the_blank_average
the_blank_average =

    30

Variable Names and Assigning Strings to Variables

some_text = 'This is some text assigned to a variable!';
some_text
some_text =

This is some text assigned to a variable!

a = 5;
b = '5';
a/b
ans =

    0.0943

a*b
ans =

   265