Live scripts are program files that contain your code, output, and formatted text together in a single interactive environment called the Live Editor. In live scripts, you can write your code and view the generated output and graphics along with the code that produced it. Add formatted text, images, hyperlinks, and equations to create an interactive narrative that you can share with others.
To open a new live script in the Live Editor, use one of these methods:
On the Home tab, click New Live Script .
Highlight commands from the Command History, right-click, and select Create Live Script.
edit function. To ensure that a live
script is created, specify a
.mlx extension. For
If an extension is not specified, MATLAB® defaults to a file with
which only supports plain code.
If you have an existing script, you can open it as a live script in the Live Editor. Opening a script as a live script creates a copy of the file and leaves the original file untouched. MATLAB converts publishing markup from the original script to formatted content in the new live script.
To open an existing script (
.m) as a live script
.mlx) from the Editor, right-click the document tab, and
scriptName as Live
Script from the context menu. You also can go to the
Editor tab, click Save, and select Save As.
Then, set the Save as type: to
Scripts (*.mlx) and click Save.
To open an existing script as a live script from the Current Folder browser, right-click the file and select Open as Live Script from the context menu.
You must use one of the described conversion methods to convert
your script to a live script. Simply renaming the script with a
.mlx extension does not work and can corrupt
Only script files can be opened as live scripts. The Live Editor does not support functions and classes.
After you create a live script, you can add code and run it. For example, add this code that plots a vector of random data and draws a horizontal line on the plot at the mean.
n = 50; r = rand(n,1); plot(r) m = mean(r); hold on plot([0,n],[m,m]) hold off title('Mean of Random Uniform Data')
By default, MATLAB autocompletes parentheses and quotes when entering code in the Live Editor. For example, type
r = rand(
r = rand()
To run the code, click the vertical striped bar to the left of the code. Alternatively, go to the Live Editor tab and in the Section section, click Run Section.
You do not need to save your live script to run it. When you do save your live
script, MATLAB automatically saves it with a
.mlx extension. For
example, to save your current live script, on the Live Editor
Save. Enter the name
plotRand. MATLAB saves the live script as
By default, MATLAB displays the output to the right of the code. Each output is displayed with the line that creates it, like in the Command Window.
You can change the size of the output display panel by dragging left or right on the resizer bar between the code and output.
To clear all output, right-click anywhere in the script and select Clear All Output. Alternatively, go to the View tab and in the Output section, click the Clear all Output button.
When scrolling, MATLAB aligns the output to the code that generates it:
To disable the alignment of output to code, right-click the output section and select Disable Synchronous Scrolling.
To reenable the alignment of output to code, right-click the output section and select Enable Synchronous Scrolling.
To view the output in line with the code, click the icon to the right of the live script. You also can go to the View tab and in the Layout section, click the Output Inline button.
To modify figures in the output, use the tools in the upper-right corner of the figure axes or in the Figure toolstrip. You can use the tools to explore the data in a figure and add formatting and annotations. For more information, see Modify Figures in Live Scripts.
To open individual outputs, such as variables and figures in a separate window, click the icon in the upper right corner of the output. Variables open in the Variables editor, and figures open in a new figure window. Changes made to variables or figures outside of a live script do no apply the output displayed in the live script.
You can add formatted text, hyperlinks, images, and equations to your live scripts
to create a presentable document to share with others. For example, add a title and
some introductory text to
Place your cursor at the top of the live script and in the Live Editor tab, select Text. A new text line appears above the code.
Click and select
Add the text
Plot Random Data.
With your cursor still in the line, click the button to center the text.
Press Enter to move to the next line.
Type the text
This script plots a vector of
random data and draws a horizontal line on the plot at the
For more information including a list of all available formatting options, see Format Live Scripts.