]>

## Variables and the Workspace

Variables in Matlab are used to name and store numeric values that will be used in the expressions that you wish to evaluate. They are different from the variables that you have used in Math classes and in Maple. While a variable in Matlab can contain any value, it must be assigned a value before it can be used in an expression. Expressions are `x+2*y-z` or `1/2*(h^2+b^2)`. The use of variables in Matlab is similar to using named memory locations in a hand-held calculator. Just as you would not use your calculator to calculate `x+2*y-z` without first assigning values to the memory locations labelled `x`, `y`, and `z`, you will not use Matlab to evaluate such an expression without first assigning values to the variable names chosen. Therefore, we will assign values to variable names and then use those variables in our calculations.

Why use variables instead of just the numeric value?

Once a value is assigned to a variable it can be reused throughout a problem without retyping the value. This is preferrable since it is easy to make typing errors when entering numeric values and there is no way for Matlab to know if a numeric value is correct or not. However, Matlab can tell if a variable name was typed correctly. We just need to correctly assign the value once and then use the variable name throughout our calculations. Assigning values to variable names makes it possible to change one value and re-solve the entire problem for the new value. It also helps us remember what each value represents in the context of our solution.

### Variable Scope (local access vs global access)

The scope of a variable is also important. The scope determines where the value of a variable is available for use in Matlab. For example, variables that you define in the main command window are available in the command window (local to that window). But, they are not available in the functions that you define. However, a user can define variables that are available in multiple scopes. By executing the `global` command with a list of variables, you make each of those variables `global` or accessible to any other block that executes the `global` statement. The value can be assigned once and then used in more than one scope. Caution: Defining all variables as `global` will likely cause more problems than it will solve. Only make a variable `global` if it must be used in more than scope and it is inconvenient to pass it to each function that requires the variable's value.

### Workspace Variables

The Matlab workspace refers to the set of variables that have been assigned values. These variables are available for use in calculating other values. Click on the Workspace tab to see the variables that have been assigned values and are available in the current scope. If the Workspace tab is not visible, select `Desktop->Workspace` from the menu tree to add it to your Desktop layout.

### Pre-Defined Variable Names

There are some values that Matlab has already assigned to variable names. For example, the value of $\pi$ has been assigned to the variable `pi`. Matlab is case-sensitive, so be sure to use `pi` and not `Pi`.

### Descriptive Variable Names

Choosing good descriptive names for each variable is important to your success in using Matlab for solving problems. Single letter variable names like `x`, `y`, and `z` are less common than more descriptive names like `height`, `width`, `tempStart`, `tempEnd`, `cost`, `price`, `time`, etc. There are several rules to follow when choosing a descriptive name for a variable.

### Variable Naming Rules (must be followed)

Variable names ...

1. must be unique in the first 63 characters*.
2. must begin with a letter.
3. may not contain blank spaces or other types of punctuation.
4. may contain any combination of letters, digits, and underscores.
5. are case-sensitive. That means that `height` is a different name than `Height`.

*Matlab uses only the first `namelengthmax=63` characters of the name to distinguish one variable from another. So be sure to keep this in mind when choosing variable and user-defined function names. User-defined functions presented in a later lesson.

Use the `genvarname` function to create names that are both valid and unique. See the `genvarname` reference page to find out how to do this.