Main Content

Create Live Scripts in the Live Editor

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.

Three live scripts demonstrating the different ways that you can combine code, output, and formatted text. One live script also demonstrates the use of a drop-down list and a slider bar in code.

Create Live Script

To create a live script in the Live Editor, go to the Home tab and click New Live Script . You also can use the edit function in the Command Window. For example, type edit penny.mlx to open or create the file penny.mlx. To ensure that a live script is created, specify a .mlx extension. If an extension is not specified, MATLAB® defaults to a file with .m extension, which only supports plain code.

Open Existing Script as Live Script

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 select Open scriptName as Live Script from the context menu. Alternatively, go to the Editor tab, click Save, and select Save As. Then, set the Save as type: to MATLAB Live Code Files (*.mlx) and click Save.


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 the file.

Add Code

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);

m = mean(r);
hold on
hold off
title('Mean of Random Uniform Data')

New live script in the Live Editor with code that plots a vector of random data and its mean

Run Code

To run the code, click the vertical striped bar to the left of the code. Alternatively, go to the Live Editor tab and click Run . While your program is running, a status indicator appears at the top left of the Editor window. A gray blinking bar to the left of a line of code indicates the line that MATLAB is evaluating. To navigate to the line MATLAB is evaluating, click the status indicator.

If an error occurs while MATLAB is running your program or if MATLAB detects a significant issue in your code, the status indicator becomes an error icon . To navigate to the error, click the icon. An error icon to the right of the line of code indicates the error. The corresponding error message is displayed as an output.

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, go the Live Editor tab, click Save , and enter the name plotRand. MATLAB saves the live script as plotRand.mlx.

Display Output

By default, MATLAB displays output to the right of the code. Each output is displayed with the line that creates it. To move focus between the code and the output using the keyboard, press Ctrl+Shift+O. On macOS, press Option+Command+O.

When scrolling, MATLAB aligns the output to the code that generates it. To disable the alignment of output to code when output is on the right, right-click the output section and select Disable Synchronous Scrolling. To change the size of the output display panel, drag the resizer bar between the code and output to the left or to the right.

Live script named plotRand.mlx with output to the right of the code

To clear an output, right-click the output or the code line that created it, and select Clear 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.

To open individual outputs, such as variables and figures, in a separate window, click the Open in figure window button 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 not apply to the output displayed in the live script.

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.

Change View

To optimize your live script for your current flow, you can change where to display output and whether to display code in the live script.

By default, output displays to the right of the code. Each output displays with the line that creates it. This option is ideal when writing code.

Live script named plotRand.mlx with code on the left and the resulting output to the right

To display output in line with the code, select the Output inline button to the right of the live script. You also can go to the View tab and in the View section, select Output inline . MATLAB displays each output underneath the line that creates it. This view is ideal for sharing.

Live script named plotRand.mlx with code at the top and the resulting output under the line that creates it

To hide the code and display only output, controls, tasks, and formatted text, select the Hide code button to the right of the live script or in the View tab. This view is ideal for sharing when you want others to only change the value of the controls in your live script, or when you do not want others to see your code.

Live script named SolarPanelEstimator.mlx with a drop-down list, two sliders, and a button that allows a user to specify inputs and start a calculation. All code is hidden.

To change the default location of the output when creating a new live script, on the Home tab, in the Environment section, click Preferences . Select MATLAB > Editor / Debugger > Display, and then select a different option for the Live Editor default view.

Format Text

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 plotRand.mlx:

  1. 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.

  2. Click the Select Style button and select Title.

  3. Add the text Plot Random Data.

  4. With your cursor still in the line, click the Align Center button to center the text.

  5. Press Enter to move to the next line.

  6. Type the text This script plots a vector of random data and draws a horizontal line on the plot at the mean.

For more information including a list of all available formatting options, see Format Text in the Live Editor.

Live script named plotRand.mlx with the centered title Plot Random Data and a line of introductory text, followed by code and its output

To adjust the displayed font size in the Live Editor, use the Ctrl + Plus (+) and Ctrl + Minus (-) keyboard shortcuts or the Ctrl + Mouse Scroll keyboard shortcut. On macOS systems, use the Command key instead of the Ctrl key.

The change in the displayed font size is not honored when exporting the live script to PDF, Microsoft® Word, HTML, or LaTeX.

Save Live Scripts as Plain Code

To save a live script as a plain code file (.m):

  1. On the Live Editor tab, in the File section, select Save > Save As....

  2. In the dialog box that appears, select MATLAB Code files (UTF-8) (*.m) as the Save as type.

  3. Click Save.

When saving, MATLAB converts all formatted content to publish markup.

Related Topics