Skip to main content
Logo image

Section 1.7 Scalar Variables

Variables need not be declared in MATLAB, one simply makes an assignment.
>> x = 5;
>> y = 10;
>> z = x + y;
When variable are assigned, they show up in the workspace window, along with their value and class (built-in debugger!)
>> x = 10; y = 10;
>> z = x + y;

Activity 1.4.

Can you name a variable "cos"? If so, what is the effect of doing so?

Subsection 1.7.1 Naming Variables

Rules for variable names (similar to C):
  • Can contain only letters, digits, and the underscore ’_’
  • Must begin with a letter (not true for C, e.g., _var)
  • Case sensitive (AA Aa aA aa are all different variables)
  • Avoid using built-in function names. You can actually use such a name, but then you can no longer use the function (i.e. cos, sin, exp, sqrt, etc.)
  • Can be up to 63 characters long… but we know that using such a variable name would be poor style!

Subsection 1.7.2 Managing Variables

Table 1.15. Managing Variables
Function Description
clear
Removes all variables from memory
clear x y z
Removes only the variables x, y, z
who
Displays a list of variables in memory
whos
Displays a list of variables in memory, along with their size and other info
The Workspace Window also shows you a visual picture of the variables that you have assigned.