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.
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.
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
script (.mlx
), use one of these methods:
From the Editor — Open the script in the Editor,
right-click the document tab, and select Open scriptName
as
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
MATLAB
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.
Note:
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 .mlx
extension.
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.
You can modify output figures interactively using the tools in the upper-right corner of each axes. These tools appear when you select or hover over the axes:
— Add data tips
to display data values.
—
Rotate the plot.
— Pan the plot.
,
— Zoom in and
out of the plot.
Update Code — Update the live script code after modifying the figure.
Copy — Copy the generated code to the clipboard. After copying the code using this tool, you can paste it into your live script to recreate the current state of the figure in code.
Note:
When you open a saved live script,
|
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 gallery. 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 |
---|---|---|
Title | Ctrl + Alt + L |
|
Heading | Ctrl + Alt + H |
|
Section break with heading | With cursor at beginning of line with Ctrl + Alt + H, then Ctrl + Alt + Enter |
|
Section break | Ctrl + Alt + Enter |
|
Bulleted list | Ctrl + Alt + U |
|
Numbered list | Ctrl + Alt + O |
|
Italic | Ctrl + I |
|
Bold | Ctrl + B |
|
Bold and italic | Ctrl + B, then Ctrl + I |
|
Monospace | Ctrl + M |
|
Underline | Ctrl + U | None |
LaTeX equation | Ctrl + Alt + G |
|
Hyperlink | 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
such as ***
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
type
##
Enter,
a heading with the word text
+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 LaTeX
equations
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.