MATLAB Script Execution: From Commands to Results

A script is a file containing MATLAB commands that you can execute together. MATLAB Script Execution lets you repeat a calculation without retyping its commands in the Command Window. To predict the result, follow the commands in execution order and track the values available at each statement. Here, you will work with ordinary .m scripts containing a straight sequence of assignments.

Run the saved commands

Write your commands in the Editor and save the file with a descriptive name, such as panel_area.m. Saving records the commands on disk; it does not perform their calculations.


To run this saved script by name, place it in MATLAB's current folder or a folder on the search path. Then enter panel_area in the Command Window, without the .m extension. You can also use the Editor's Run button. Save your latest edits before running so that the saved file contains the calculation you intend to execute.


Choose a filename that does not conflict with an existing variable or another code file. Opening a script in the Editor and executing it are separate actions.


Three connected boxes show saved commands being executed in order and leaving updated workspace values.


Track assignments in order

For these straight-line scripts, MATLAB executes statements from top to bottom. At an assignment, it evaluates the right-hand expression using the values available at that moment, then stores the result in the variable on the left.


new stored value←expression evaluated using current values\text{new stored value} \leftarrow \text{expression evaluated using current values}


Example: A panel calculation uses the following saved commands. The variable names identify the intended units; MATLAB stores the numerical values without automatically attaching those units.


MATLAB
length_m = 6;
width_m = 2;
area_m2 = length_m * width_m;
length_m = 9;


When the third statement executes, the area is (6 m)(2 m)=12 m2(6\ \text{m})(2\ \text{m})=12\ \text{m}^2. The fourth statement changes length_m to 9, but area_m2 remains 12. An assignment stores a value at execution time; it does not create a permanently updating relationship between variables.



A semicolon suppresses the automatic display of an assignment's result. The calculation and storage still happen. A quiet Command Window therefore does not mean that the script failed to execute.

Understand the workspace and reruns

The base workspace is the collection of variables shared by the Command Window and scripts run from it. Such a script can read existing variables, replace their values, and create new variables. Its completed assignments remain available after the run.


Running a script again starts its commands again, but does not automatically reset existing variables. A script that assigns all its inputs before using them can avoid depending on values left by earlier work. A script that reads an existing variable may produce a different result on its next run.



An ordinary script without error handling stops if it encounters a runtime error, such as using an undefined variable. Earlier completed assignments are not automatically undone.


Event

Effect on workspace values

Save the file without running it

No assignments execute

Complete a successful run

Completed assignments remain available

Stop at an undefined-variable error

Earlier assignments remain; later statements do not execute


What to remember

  • Save the intended commands and make the script accessible before running it by name.
  • Evaluate assignments in execution order using the values currently stored.
  • A semicolon suppresses automatic output, not execution.
  • Rerunning starts the commands again using the workspace then available.
  • After an error, distinguish completed assignments from statements that never executed.

Example: MATLAB Script Execution for an Energy Estimate

An engineer estimates the energy delivered by a small heater. An execution trace records how stored variable values change as each statement runs. Use one to follow this saved file, heater_energy.m, which is available in MATLAB's current folder and has no naming conflicts.


MATLAB
power_W = 150;
duration_s = 40;
energy_J = power_W * duration_s;
duration_s = 60;
delivered_J = 0.8 * energy_J;


Assume a constant power and a delivered-energy fraction of 0.8. Run the entire file from the Command Window. The variable names indicate watts, seconds, and joules.

First run

a) Find the stored value of energy_J and the final value of duration_s after the run.


The first two statements assign a power of ________ and an initial duration of ________. The third statement uses those values immediately:


E=Pt=(150 W)(40 s)=6000 J.E=Pt=(150\ \text{W})(40\ \text{s})=6000\ \text{J}.


The fourth statement then replaces the duration with 60 s. It does not execute the energy calculation again. No later statement assigns to energy_J or duration_s.


The stored energy is 6000 J, and the final duration is 60 s.


b) Find the final value of delivered_J. Explain why the script does not use 60 s to obtain this result.


The last statement reads the energy already stored by the third statement. Its delivered-energy fraction is ________, so:


Edelivered=0.8E=0.8(6000 J)=4800 J.E_{\text{delivered}}=0.8E=0.8(6000\ \text{J})=4800\ \text{J}.


The later duration assignment does not update the stored energy. Although duration_s now contains 60, the final statement reads energy_J, which still contains 6000.


The final value of delivered_J is 4800 J.

Edit and rerun

c) Change only the second statement to duration_s = 50;, save the file, and run the entire script again. Starting with the workspace left by the first run, find the new final value of delivered_J.


The new run begins at the first statement. It assigns 150 W again, and the edited second statement assigns a duration of ________, replacing the 60 s retained from the earlier run.


The third statement recalculates the energy:


E=(150 W)(50 s)=7500 J.E=(150\ \text{W})(50\ \text{s})=7500\ \text{J}.


The energy available to the final calculation is therefore ________. The fourth statement again sets the duration to 60 s, without changing that stored energy. The final statement gives:


Edelivered=0.8(7500 J)=6000 J.E_{\text{delivered}}=0.8(7500\ \text{J})=6000\ \text{J}.


The new final value of delivered_J is 6000 J. The earlier workspace does not determine these inputs because the script assigns them before using them.

A student saves lamp_energy.m in MATLAB's current folder and runs it by entering lamp_energy in the Command Window. No naming conflicts exist. The file contains:


MATLAB
power_W = 90;
time_s = 8;
energy_J = power_W * time_s;


The Command Window displays no calculated values. Which statement correctly describes the result of this complete, successful run?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A laboratory student writes several MATLAB commands to estimate a cooling time and chooses coolingEstimate as the file's base name. The student wants a standard plain-text script, rather than a live script.


Complete these instructions. Enter only the extension letters in the first blank and only the base name in the second.


Save the file with the extension .. With that file in MATLAB's current folder, enter in the Command Window and press Enter to execute it. Assume no variable or other callable file has the same name.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A motion calculation is saved as the following MATLAB script. Run the entire file once, with no other commands executed during the run.


MATLAB
speed_mps = 2.4;
time_s = 15;
distance_m = speed_mps * time_s;
speed_mps = 3;
distance_m = distance_m + speed_mps * 5;


The final statement adds the distance travelled during another 5 s. What is the final value of distance_m? Answer in metres, to one decimal place.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice