Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

MATLAB Code: Write, Save and Run Scripts and Live Scripts

MATLAB code explained: how scripts, functions and live scripts differ, and the exact menu steps to create, run and save each one.

Written and maintained by CASRAI Editorial Board

Last updated

MATLAB code is a set of commands saved in a file that MATLAB runs for you. The three kinds of file you will meet are scripts, functions and live scripts. This page shows how to create each one, how to run it, and how to tell which one to use, with the menu names and commands as written in the MathWorks documentation (read 2026-11-08).

What MATLAB code is used for in research

A script stores commands exactly as you would type them at the command line, so anything you do interactively (loading data, fitting a model, drawing a figure) can be saved and repeated. Saving the steps in a file gives you a record of the analysis that can be rerun and shared. For what the software is typically applied to, see what MATLAB is used for.

MATLAB code language and “MATLAB codes”

The files described below contain statements written in the MATLAB language. “MATLAB codes” in search usually means such files: scripts, functions or live scripts that you write or receive from someone else. Each type is covered in the sections that follow.

Install or access

You need a working MATLAB session to run anything below. Installation is covered in MATLAB installation, and licence types in MATLAB licensing. Use the official MathWorks site as the source for the software.

Script, function or live script: which one are you writing?

Type What it is Arguments Workspace
Script Stores commands exactly as you would type them at the command line None; edit the values in the file Base workspace
Function Declared with a line such as function a = triarea(b,h), saved as triarea.m and called by name Input and output arguments Its own workspace, separate from the base workspace
Live script A script opened in the Live Editor, where output appears next to the code See the source page See the source page

Source: MathWorks, “Scripts and Functions” and “Create Live Scripts” (read 2026-11-08).

A worked reading of the difference: the MathWorks example runs a script called triarea, and the result is stored in the variable a in the base workspace, where you can see it after the script ends. A function does not do this, because it has its own workspace. If you want code to take different inputs each time, write a function; see how to declare a function in MATLAB.

Step-by-step: create and run your first script

Create the file

Any one of these routes creates a script:

  1. In the Command History, right-click and choose Create Script.
  2. On the Home tab, click New Script.
  3. At the command line, type edit new_file_name. This creates and opens the file. Typing edit alone opens a file called “Untitled”.

Run it

  1. Type the script name at the command line and press Enter (the MathWorks example is numGenerator).
  2. Or, on the Editor tab, click Run.
  3. Or call it from another code file with a line such as numGenerator;.

Source: MathWorks, “Create Scripts” (read 2026-11-08). Saving files and variables is covered separately in saving in MATLAB.

Create and run a live script

  1. On the Home tab, click New Live Script.
  2. To convert an existing script, right-click its document tab and select Open scriptName as Live Script. This makes a copy.
  3. To run one section, use the striped bar to the left of the code. To run everything, click Run on the Live Editor tab. You do not need to save a live script to run it.
  4. To save, click Save on the Live Editor tab, or press Ctrl+S.

Source: MathWorks, “Create Live Scripts” (read 2026-11-08).

Output and how to read it

In a live script, output appears to the right of the code by default. On the View tab you can choose Output on Right, Output Inline or Hide Code, and the documentation lists Clear All Output for removing results. For a plain script, results go to the base workspace and the Command Window, as in the triarea example. For figures and plots see the MATLAB documentation guide; for building an interface around your code see graphical user interfaces in MATLAB.

Troubleshooting

  • An error in a live script. An error icon appears to the right of the line of code, and the matching error message appears as output. Click the icon to navigate to the error (MathWorks, “Create Live Scripts”).
  • A script needs different values. Per the MathWorks documentation a script is edited in the file rather than called with arguments, so edit the values in the file, or move the code into a function.

Free alternatives

If you do not have a MATLAB licence, these free and open-source projects can run similar numerical work. They are different languages or environments, so code is not interchangeable except where the project states compatibility. The descriptions below are the projects’ own wording.

Alternative How the project describes itself Licence stated Official source (read 2026-11-08)
GNU Octave A scientific programming language with mathematics-oriented syntax and built-in 2D/3D plotting; syntax “largely compatible with Matlab” and “drop-in compatible with many Matlab scripts” GNU General Public License octave.org
NumPy (Python) “The fundamental package for scientific computing with Python” BSD numpy.org
R “A language and environment for statistical computing and graphics” GNU General Public License r-project.org
Julia A dynamically typed language designed for high performance, which compiles to native code via LLVM MIT julialang.org

Octave is the only one of these that states MATLAB-script compatibility, and the project says “many”, not all, scripts. Test any script you plan to move before relying on its results.

FAQ

How do I write my first MATLAB code?

Click New Script on the Home tab, type the commands you would normally enter at the command line, save the file, and click Run on the Editor tab.

What is the difference between a script and a function?

A script stores commands as typed at the command line and uses the base workspace, with no arguments. A function is declared with a function line, takes arguments, and has its own workspace.

Can I run MATLAB code from another file?

Yes. Call the script by name in a line of the other file, for example numGenerator;.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about MATLAB Code: Write, Save and Run Scripts and Live Scripts

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →