Live Scripts and the Live Editor
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. The reference material consulted while preparing these pages is listed in the section bibliography. |
Written against MATLAB R2026b. The Live Editor is a graphical part of the MATLAB desktop, so none of it runs in GNU Octave, which has no live scripts, no controls and no tasks; everything about the Live Editor below is checked against the R2026b documentation ("not run here"). One thing can be run: a plain text live script is an ordinary .m file in which the extra content lives in comments, so Octave 11.3.0 executes its code. The one session below that shows output was run that way, and it says so.
A live script is a program file that holds code, its output and formatted text together in one interactive document. You write the code, run it, and see the result (numbers, tables, figures) beside or below the lines that produced it. A live function is the same idea for a function file. Live scripts suit exploration, teaching and reports; plain scripts (see Scripts and Code Sections) suit code that is edited as text, run in automation or reviewed in a diff. The page shows the heatflow time-stepping script of the scenario as a live script (see the heatflow scenario).
Creating and running a live script
On the Home tab click New Live Script. If you already have a script, you can open it as a live script: right-click the document tab and choose the option to open it as a live script, or use Save As with the type MATLAB Live Code File. The documentation says opening a script this way creates a copy and leaves the original untouched, and converts publishing markup into formatted content. A live function is created from the New menu in the same way.
Running works on two scales. Click the striped bar left of a section to run that section, or use Run on the Live Editor tab to run the whole file. A status indicator at the top left shows the line being evaluated, and turns into an error icon if MATLAB finds a problem; a click on it jumps to the error, whose message appears as an output. You do not have to save a live script before running it. Save with the Save button or Ctrl+S.
Output appears in one of three views, chosen on the View tab (and for new files in the Editor/Debugger display settings):
| View | Use |
|---|---|
Output on right |
Each output sits beside the line that made it, and scrolls with it. The documentation recommends it while writing code. |
Output inline |
Each output sits under its line. Recommended for sharing. |
Hide code |
Only output, labeled controls, tasks and formatted text are shown. For readers who should change control values but not read or edit the code. |
Individual outputs can be cleared (Clear Output, Clear All Output) or opened in a separate window: variables open in the Variables editor, figures in a figure window. Changing a variable or figure outside the live script does not change the output that the script shows. Figures in the output have an axes toolbar for zooming, rotating and adding annotations (see Graphics Objects and Customization).
File formats: plain text live code and .mlx
MATLAB saves live scripts in the plain text live code format (.m) by default (documented under R2026b; the format arrived in R2025a). The older binary live code format (.mlx) stores the content in a binary structure and is still available. The two hold the same features: output, formatted text, equations, controls and tasks.
| Aspect | Plain text live code .m |
Binary live code .mlx |
|---|---|---|
Extension |
|
|
Readable outside MATLAB |
Yes, in any text editor (images and plots are stored as Base64 text) |
No, the structure is designed for use in MATLAB |
Source control |
Compare and merge with external tools, because the content is text |
A binary file: register it as binary in the version-control system, and merging is not meaningful (see Projects and Source Control) |
Introduced |
R2025a, and the default in R2026b |
R2016a (live scripts; live functions followed in R2018a), the default until R2026b |
In the plain text format, text, tables, equations and hyperlinks are stored inline as comments with a Markdown-based markup, and everything that does not fit inline (output data, the current view) goes into an appendix at the end of the file. Because the markup lives in comments, a plain text live script is still a script: MATLAB, and as the session below shows Octave, run the lines that are not markup. The documentation lists these markup forms:
| Content | Markup in the file |
|---|---|
Section break |
A line with only |
Formatted text |
Lines that start |
Lists |
Lines such as |
Table of contents |
The |
Tables, images, links |
|
Equations |
LaTeX commands inside the |
Appendix |
Starts at a |
Two rules in the documentation matter for anyone who edits such a file by hand. A line that is not markup, including a blank line, is code, so blank lines must not be put between markup lines. And editing the file as text can leave it unable to open correctly if the markup structure becomes invalid; to open a live script as text, right-click the file in the Files panel and choose Open as Text. When two versions are merged, the reference IDs that tie inline markup to appendix data must stay consistent.
The heatflow time-stepping script, saved as plain text live code in the file heatflow_live.m, looks like this. It is shown here as a text file; opened in the Live Editor it displays a title, an equation, two headed sections and the output.
%[text] # Heat diffusion on a plate
%[text] Explicit time stepping with the stability limit $\Delta t \le \Delta x^2 / (4\alpha)$.
%%
%[text] ## Parameters
n = 5;
alpha = 1e-4;
dx = 0.1;
dtMax = dx^2/(4*alpha);
dt = 0.8*dtMax;
%[text] Each step uses 80 percent of the largest stable step.
%%
%[text] ## Step the field
T = zeros(n);
T(:,1) = 100;
for k = 1:200
Tn = T;
Tn(2:end-1,2:end-1) = T(2:end-1,2:end-1) + alpha*dt/dx^2 * ( ...
T(1:end-2,2:end-1) + T(3:end,2:end-1) + ...
T(2:end-1,1:end-2) + T(2:end-1,3:end) - 4*T(2:end-1,2:end-1));
T = Tn;
end
fprintf("dtMax = %g, dt = %g\n", dtMax, dt);
fprintf("%.4f\n", T(3,2:4));
%[appendix]{"version":"1.0"}
%---
The two last lines are a minimal stand-in for the appendix that MATLAB writes in a real file (the exact appendix content is not reproduced here). The script takes the 5 by 5 plate with its left edge held at 100 degrees, steps it 200 times at 80 percent of the stability limit, and prints the middle row of interior values. Run as an ordinary script in Octave 11.3.0, where the markup is just comments:
Octave 11.3.0 output (MATLAB layout may differ):
dtMax = 25, dt = 20
52.6786
25.0000
9.8214
A second check on the same file counts the lines that start with the markup prefix.
txt = fileread("heatflow_live.m");
lines = strsplit(txt, "\n");
isMk = strncmp(lines, "%[", 2);
fprintf("%d lines, %d markup lines\n", numel(lines), sum(isMk));
fprintf("%s\n", lines{find(isMk,1)});
Octave 11.3.0 output:
26 lines, 6 markup lines
%[text] # Heat diffusion on a plate
Formatting text and inserting equations
Formatted text is added with the Text button on the Live Editor tab (a text line above or below the cursor). The Select Style button offers styles such as title, headings, and body text; the toolstrip adds bold, italic, monospace and underline, alignment, lists, hyperlinks, images and a table of contents. The exact list of styles and options is on the Format Text page (Format Text in Live Scripts); only the points above were checked for this page.
Equations are placed on text lines only. If you insert an equation while the cursor is on a code line, MATLAB puts it on a new text line directly below. There are two ways:
| Way | Notes |
|---|---|
Interactive |
On the Insert tab choose Equation and build it from the symbol and structure galleries. Typing a backslash and a name (for example |
LaTeX |
Insert > Equation > LaTeX Equation opens a dialog where you type LaTeX, with a preview. The Alt Text box sets the description that is saved in the |
The stability limit line of the file above is a LaTeX equation. In the plain text file it is stored inline as $...$ LaTeX in a %[text] line, as the listing shows.
Interactive controls
Controls let a reader of the live script change a variable without editing code. Select a value (or place the cursor next to it), open Control on the Live Editor tab and choose one; right-click and Configure Control sets its range, items and labels. Replacing one control by another keeps relevant settings such as range and default.
| Control | Sets the variable to |
|---|---|
Slider, Numeric Spinner |
A number within a range |
Check Box |
The logical value |
Drop-Down List |
One of a list of items; labels and values are configured separately, and text values must be quoted |
Edit Field |
Any typed input, converted to the configured data type |
Button |
Nothing by itself; it runs code when clicked (the Execution section selects what runs) |
File Browser |
The path of a file or folder (since R2023a) |
Color Picker |
A color, in a configurable format such as RGB or hexadecimal (since R2023b) |
Date Picker |
A date (since R2024a) |
For heatflow, a slider for the stability factor (0.1 to 1) in place of the literal 0.8 makes the stability limit tangible: set it above 1 and the field blows up. A hidden-code view of that script, with only the slider and the plot visible, is a good way to hand the simulation to a colleague. When a live script containing controls is exported, the controls are saved as code in the exported file.
Live Editor tasks
A task is a point-and-click interface embedded in a live script that performs a set of operations and generates the MATLAB code for them. You insert one from the Task button on the Live Editor tab, or by typing its name in a code block and picking the completion. The documentation example inserts Create Plot and Clean Outlier Data on a vector with outliers; the cleaned result is stored in a workspace variable named in the task. Show the generated code to see (and copy) the commands that the task runs.
Behaviours worth knowing:
-
Autorun. By default, changing a parameter reruns the task and its current section, which keeps the section up to date. Clear the Autorun check box to rerun by hand with the Run current section button. Some tasks do not autorun by default, to keep them responsive.
-
Run Configuration (in the Options menu) changes which code runs when a parameter changes.
-
Collapse a task to a single readable line of pseudocode with its output; Restore Default Values resets the parameters; Remove Task deletes it.
-
Tasks can be written by users: a subclass of
matlab.task.LiveTask, or a conversion of a selection that contains code and controls (the latter since R2023b). The building page is Building Apps with App Designer.
Many tasks belong to toolboxes; base MATLAB ships tasks such as Create Plot and Clean Outlier Data (see Toolboxes and Add-On Products for products).
Sharing and exporting
You can share the live code file itself: the recipient opens it in MATLAB in the state you last saved, with generated output. A plain text .m file can also be opened in any text editor. Otherwise, export to a document that opens outside MATLAB:
| Format | Notes |
|---|---|
PDF, Microsoft Word, HTML, LaTeX |
On the Live Editor tab choose Export; the Export dialog sets paper size, orientation and margins for PDF, Word and LaTeX (since R2023b). Exporting to LaTeX writes a |
Markdown, Jupyter notebook |
Export to Markdown ( |
Folder |
Export > Export Folder converts every live script and function in a folder. |
Program |
|
Figures are exported in the resolution and format you choose, but that needs the file to have been run before exporting. Controls and tasks are saved as code in the converted file; if you hide the code before exporting, the converted file does not contain them. To present a file, the Full Screen button on the View tab (Ctrl+F11, or Command+F11 on macOS) shows it full screen.
Hiding code is also the documentation’s advice before sharing a live script, both to focus the reader on the output and when you do not want others to see the code. It is presentation only, not a way to protect code; for that see Secrets and Protecting Code.
Publishing a script (the classic way)
Before live scripts, and still supported for plain .m files, MATLAB can publish a script divided into %% sections to a formatted document that contains the code, the formatted comments and the output. Typical examples are teaching and external documentation of code. This is the form that older books describe (Davis, chapter 23, in the MATLAB Primer checklist).
Write the script with %% at the start of each section, and a title after it; explanatory comments below the section line become text. Markup in those comments formats the result: a title or heading line after %%, underscores around a word for italics (_k_), and a ... LaTeX equation. On the Publish tab click Publish; by default MATLAB creates a subfolder named html containing an HTML file and a file for each figure. Other output formats exist (for example PDF) and are selected in the publishing options; the publish function does the same from code; it runs the file in the base workspace and, by default, writes HTML to the html subfolder.
%% Heat Diffusion on a Plate
% Explicit time stepping of the plate temperature with the stability
% limit $$ \Delta t \le \Delta x^2 / (4 \alpha) $$.
%% Parameters
alpha = 1e-4;
dx = 0.1;
dt = 0.8 * dx^2 / (4*alpha);
%% Result
plot(0:3, [100 52.7 25 9.8])
title("Temperature along a row")
publish("heatflowPublished.m", "html")
Not run here (Octave 11.3.0 has no publish function in its core). The result lies in the html folder next to the file. A published document is static: unlike an exported live script it cannot contain controls the reader can change.
Choosing between scripts, live scripts and publish
| Question | Script .m |
Live script | Published script |
|---|---|---|---|
Output shown with code |
No, in the Command Window |
Yes, inline or on the right |
Yes, in the generated document |
Reader interaction |
None |
Controls and tasks |
None |
Best in automation ( |
Yes |
Possible, but output and tasks are for the interactive editor; use a plain script for CI |
Not applicable |
Text diff in review |
Clean |
Clean only for the plain text format |
Clean (the source is a script) |
For heatflow, keep the time-stepping logic in a function (the namespace page covers this), call it from a plain script such as runHeatflow in the build and the tests, and use a live script as the explanatory notebook for people.
Pitfalls
-
Treating
.mlxas text. A binary live code file is not mergeable by line; prefer the plain text format for projects under version control, and register.mlxas binary if you keep any. -
Editing plain text live code carelessly. A blank line between markup lines becomes an empty code statement and can break the layout; a mismatched reference ID separates an output from its data.
-
Missing functions in a shared live script. A recipient whose path lacks a function the script calls sees the error as an output under the line, for example "Unrecognized function or variable 'laplacian'." with identifier
MATLAB:UndefinedFunction; ship the code with the script, or a project that sets the path. -
Stale output. Output saved in the file reflects the last run; edited code does not update it until you run again. Changing a variable in the workspace does not change the displayed output.
-
Interactive code. Code that waits for input in a dialog is awkward in a document meant to be read; use a control instead.
-
Using a live script for automation.
matlab -batchcan run a plain text live script like any.mfile, but a script with controls or tasks is designed for an interactive session. -
Confusing the two
%%forms. In a plain text live script a%%line is a section break; in a published script it also starts a section but is followed by a title that publishing turns into a heading.
Where older books differ
Older books describe the Editor, %% cells and publishing; none describes live scripts with the .mlx format (R2016a), nor plain text live code (R2025a), controls, tasks or the export options. The cell mode and publish forms are still valid. Where a book tells you to publish a report to HTML to share results, a live script exported to PDF, HTML or Markdown now gives a similar result with less setup, and the plain text format makes it usable with source control.