Skip to content Skip to navigation

Connexions

You are here: Home » Content » Variables in M-file Environments

Navigation

Recently Viewed

This feature requires Javascript to be enabled.

Variables in M-file Environments

Module by: Darryl Morrell. E-mail the author

User rating (How does the rating system work?)
Ratings

Ratings allow you to judge the quality of modules. If other users have ranked the module then its average rating is displayed below. Ratings are calculated on a scale from one star (Poor) to five stars (Excellent).

How to rate a module

Hover over the star that corresponds to the rating you wish to assign. Click on the star to add your rating. Your rating should be based on the quality of the content. You must have an account and be logged in to rate content.

:
(0 ratings)

Summary: This module provides a brief introduction to the use of variables in m-file environments.

Note: Your browser may not currently support MathML. See our browser support page for additional details. You can always view the correct math in the PDF version.

Variables

A variable is a named storage location that can be set to a particular value which can be used in subsequent computations. For example, we store a value of 5 in the variable a with the statement a=5. This value remains in a until we store a different value (for example, using the command a=100) or we clear a using the command clear a. Once a variable is set to a particular value, we can get this value by using the variable name in an expression (e.g. a/2).

Example 1

Suppose we wish to compute the circumference of a circle of diameter 5 units using the formula c = π d c π d . We could first set the variable d to a value of 5:


>> d = 5

d =

    5.000

Then we could compute the circumference and assign its value to the variable c:

>> c = pi*d

c =

    15.708

In this command, the product of the value of d (which is known because we earlier set it to 5) and the value of pi (which is a pre defined variable) is computed and the value of the product is stored in the variable c.

Variable names must begin with an upper- or lower-case letter. They may contain letters, digits, and underscores; they may not contain spaces or punctuation characters. Variable names are case sensitive, so A and a are different variables.

Exercise 1

Valid variable names

Which of the following are valid variable names?

  1. a
  2. B
  3. ecky_ecky_ecky_ecky_ptang_zoo_boing
  4. ecky ecky ecky ecky ptang zoo boing
  5. 2nd
  6. John-Bigboote

Solution

  1. Valid.
  2. Valid.
  3. Valid.
  4. Invalid, because the variable name contains spaces.
  5. Invalid, because the variable name begins with a number.
  6. Invalid, because the variable name contains a dash.

There are several predefined variables. The most commonly used include

  • ans - the default variable in which computation results are stored.
  • pi - π.
  • i or j - -1 -1 .
Once assigned, variable names remain until they are reassigned or eliminated by the clear command.

Variables can contain several types of numerical values. These types include the following:

  • Scalar - a scalar is a single value (i.e. a number). c and d in Example 1 are scalar variables.
  • Vector - a vector is an ordered series of numbers.
  • Matrices - a matrix is a rectangular array of numbers. The ability to do computations on vectors and matrices gives MATLAB its name (MATrix LABoratory).
  • strings - variables may also contain strings of characters.

Content actions

Give Feedback:

E-mail the module author | Rate module ( How does the rating system work?)

Rating system

Ratings

Ratings allow you to judge the quality of modules. If other users have ranked the module then its average rating is displayed below. Ratings are calculated on a scale from one star (Poor) to five stars (Excellent).

How to rate a module

Hover over the star that corresponds to the rating you wish to assign. Click on the star to add your rating. Your rating should be based on the quality of the content. You must have an account and be logged in to rate content.

(0 ratings)

Download:

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections directly in Connexions. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need a Connexions account to use 'My Favorites'.

| A lens (?)

Definition of a lens

Lenses

A lens is a custom view of Connexions content. You can think of it as a fancy kind of list that will let you see Connexions through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to Connexions materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual Connexions member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks