Chapter 4: Writing Scripts


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.

Variable named “n” being assigned the value of [latex]6.02{\times10}^{23}[/latex].

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

images 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.



  1. Define two variables a and b which are assigned the values 12 and 3, respectively. Create a code that outputs the values for a divided by b, a multiplied by b, and a to the b power. Assign the outputs the values x, y, and z, respectively.


Note that the answer displayed in the command window only depicts values that were not suppressed in the code by semicolons at the end of a line.





2. The Pythagorean theorem can be used to determine the length of the right triangle’s sides. Write a MATLAB script that computes the hypotenuse for a right triangle with short sides x = 3 and y = 5. The most useful form of the Pythagorean theorem is provided.

[latex]Hypotenuse=\ \sqrt{x^2+y^2}[/latex]


  1. The following equation can be used to calculate the distance that an object will free fall over some amount of time. Assuming that the object starts at zero velocity and has no air resistance, create a code that can calculate the total distance traveled by an item in free fall for a given gravitational acceleration, [latex]g\ (\frac{meters}{{second}^2}),[/latex] and time, [latex]t\ (seconds).[/latex]

[latex]∆h= 1/2 gt^2[/latex]


2. An engineer would like to easily calculate the volume and surface area of a classroom in a building. Create a script file within MATLAB that can calculate the volume of the room and the surface area of wall #1, wall #2, and the floor, as labeled in the image, by assigning values to variables x, y, and z, and completing arithmetic operations with them. Use comments to label each part of the script.


3. A scientist working with high-power electron microscopes would like to convert units from micrometers and nanometers to meters so students can understand how small these units are. Create a MATLAB script that converts a variable with units in meters to micrometers [latex](μm)[/latex] and nanometers [latex](nm)[/latex]. The necessary conversion factors are provided below. Use comments to label your code.

[latex]10^6 μm=1 m[/latex]

[latex]10^9 nm=1 m[/latex]



Icon for the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

A Guide to MATLAB for ME 160 by Austin Bray and Reza Montazami is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, except where otherwise noted.

Share This Book