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 with the code that produced it. Add formatted text, images, hyperlinks, and equations to create an interactive narrative that can be shared with others.
To open a new live script, use one of these methods:
On the Home tab, in the New drop-down menu, select 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
If an extension is not specified, MATLAB® defaults to a
.m extension, which only supports plain
If you have existing scripts, you can open them as live scripts. 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.
Only script files can be opened as live scripts. Functions and classes are not supported in the Live Editor, and cannot be converted.
To open an existing script (
.m) as a live
.mlx), use one of these methods:
From the Editor — Open the script in the Editor,
right-click the document tab, and select Open
Live Script from the context menu. You can also go to
the Editor tab, click Save
, and select Save
As. Then, set the Save as type: to
Live Scripts (*.mlx) and click Save.
From the Current Folder browser — Right-click the file in the Current Folder browser and select Open as Live Script from the context menu.
You must use one of the described conversion methods to convert
your script into a live script. Simply renaming the script with a
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')
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 Run 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
By default, MATLAB displays the output to the right of the code. Each output displays with the line that creates it, like in the Command Window.
To move the output in line with the code, use either of these methods:
In top right of the Editor window, click the icon.
Go to the View tab and in the Layout section, click the Output Inline button.
You can further modify the output display in these ways:
Change the size of the output display panel — With output on the right, drag left or right on the resizer bar between the code and output.
Clear all output — Right-click 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.
Disable the alignment of output to code — With output on the right, right-click the output section and select Disable Synchronous Scrolling.
Open output 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.
Modify figures interactively — Use the tools in the upper-right corner of the figure axes or in the Figure toolstrip to explore the data and add formatting and annotations. For more information, see Modify Figures in Live Scripts.
You can add formatted text, hyperlinks, images, and equations to your live scripts to create a presentable document to share with others.
To insert an item, go to the Live Editor tab and in the Insert section, select one of these options:
Code — This inserts a blank line of code into your live script. You can insert a code line before, after, or between text lines.
Text — This inserts a blank line of text into your live script. A text line can contain formatted text, hyperlinks, images, or equations. You can insert a text line before, after, or between code lines.
Section Break — This inserts a section break into your live script. Insert a section break to divide your live script into manageable sections that you can evaluate individually. In live scripts, a section can consist of code, text, and output. For more information, see Run Sections in Live Scripts.
Equation — This inserts an equation into your live script. Equations can only be added in text lines. If you insert an equation into a code line, MATLAB places the equation in a new text line directly under the selected code line. For more information, see Insert Equations into Live Scripts
Hyperlink — This inserts a hyperlink into your live script. Hyperlinks can only be added in text lines. If you insert a hyperlink into a code line, MATLAB places the hyperlink in a new text line directly under the selected code line.
Image — This inserts an image into your live script. Images can only be added in text lines. If you insert an image into a code line, MATLAB places the image in a new text line directly under the selected code line.
You can further format text using any of the styles included in the Text Style section. Use the down arrow to the right of the section to display all the available text style options. Styles include Normal, Heading, Title, Bulleted List, and Numbered List.
You also can apply standard formatting options from the Format section, including bold , italic , underline , and monospace .
For quick formatting in live scripts, you can use a combination of keyboard shortcuts and character sequences. Formatting appears after the final character in a sequence has been entered.
This table shows a list of formatting styles and their available keyboard shortcuts and autoformatting sequences.
|Formatting Style||Keyboard Shortcut||Autoformatting sequence|
Ctrl + Alt + L
Ctrl + Alt + H
|Section break with heading|
With cursor at beginning of line with
Ctrl + Alt + H, then Ctrl + Alt + Enter
Ctrl + Alt + Enter
Ctrl + Alt + U
Ctrl + Alt + O
Ctrl + I
Ctrl + B
|Bold and italic|
Ctrl + B, then Ctrl + I
Ctrl + M
Ctrl + U
Ctrl + Alt + G
Ctrl + K
Note: Title, heading, section break, and list sequences must be entered at the beginning of a line.
There may be times when you want an autoformatting sequence
*** to appear literally. To display the
characters in the sequence, escape out of the autoformatting by pressing
the Backspace key or by clicking Undo
. For example, if you
a heading with the word
text appears. To undo the
formatting style and simply display
## text, press
the Backspace key. You can only escape out of a sequence
directly after completing it. Once another character is entered, or
the cursor is moved, escaping is no longer possible.
To revert the autoformatting for
and hyperlinks at any point, use the Backspace key.
To force formatting to reappear after escaping out of a sequence, click the Redo button. You can only redo an action directly after escaping it. Once another character is entered, or the cursor is moved, the redo action is no longer possible. In this case, to force the formatting to reappear, delete the last character in the sequence and type it once again.
To disable all or certain autoformatting sequences, you can adjust the Editor/Debugger Autoformatting Preferences.