Scripts are a first step toward thinking programmatically: instead of typing commands one at a time in the Command Window, you write a repeatable recipe that you can run again and again to get the same results. This also helps you organize work into clear stages: define inputs, perform computations, and display results.
% Script: example_script.m
% Purpose: Demonstrate a repeatable sequence of commands.
% 1) Set up a clean run
clc;
clear;
% 2) Define inputs (numbers you choose)
input1 = 10;
input2 = 3;
% 3) Compute results
result = input1^2 + 2*input2;
% 4) Display final results
fprintf('Result = %g\n', result);
π:βRunningβ a Script.
A script shares the same workspace as the Command Window. That means variables created in the script appear in the Workspace after the script runs, and existing variables in the Workspace can affect a script if you rely on them. This is why scripts often begin with clear to avoid old values.
Later, we will introduce functions, which take inputs and produce outputs in separate workspaces. For now, scripts help us practice building a correct sequence of commands.