"

2. Variables

Variables are critical pieces of MATLAB codes that users can assign values to when writing scripts. This chapter will address uses of variables to solve algebraic operations and to analyze data. The following image shows an example of the format for creating a variable. In this example, the variable’s name is the letter ’n’ and the variable’s value is 6.02e23. A variable has value since MATLAB will know that everywhere in a script where ‘n’ is typed the value 6.02e23 is transferred. This functionality at its simplest form enables users to keep code clean by writing variables instead of a number repeatedly. Importantly, if the user changes the value of the variable where it is initially defined in a code, then the value changes everywhere else in a code.

Figure 2.2.1 Variable named “n” being assigned the value of .
Figure 2.2.2 Example of variables being assigned in the command window.

Variable Operations

Once variables have values assigned to them, they become useful tools for numeric operations, and data analysis, and will be critical in functions, which will be addressed next. The following show examples of how variables can be used in numeric and matrix applications. Once variables have been assigned numeric values, the user can view the workspace window to check the value assigned to each variable. This feature is valuable for long codes with many variables, as the user is able to check the value for each variable and keep track of which variable names have been used.

When creating equations within MATLAB codes, values assigned to variables can be checked to verify if equations are operating how the user intended them to. For example, the user can manually calculate the expected values generated by an equation and can check actual values displayed in the workspace to ensure that a code functions as intended by the user.