Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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.

Open New Live Script

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.

  • Use the edit function. To ensure that a live script is created, specify a .mlx extension. For example:

    edit penny.mlx

    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. You also can go to the Editor tab, click Save, and select Save As. Then, set the Save as type: to MATLAB Live 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.

Note

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

  • Only script files can be opened as live scripts. The Live Editor does not support functions and classes.

Run 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);
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(
MATLAB automatically adds the closing parenthesis to the statement.
r = rand()
MATLAB also autocompletes comments, character vectors, and strings when split across two lines. To escape out of an autocompletion, press Ctrl+Z or the Undo button . Autocompletions are enabled by default. To disable them, see Editor/Debugger Autocoding Preferences.

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 tab, click Save. Enter the name plotRand. MATLAB saves the live script as plotRand.mlx.

Display Output

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.

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 and select Title.

  3. Add the text Plot Random Data.

  4. With your cursor still in the line, click the 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 Live Scripts.

Related Topics

Was this topic helpful?