Main Content

创建押尾学e 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.

创建押尾学e Script

To create a live script in the Live Editor, go to theHometab and clickNew Live Script. You also can use theeditfunction in the Command Window. For example, typeedit penny.mlxto open or create the filepenny.mlx. To ensure that a live script is created, specify a.mlxextension. If an extension is not specified, MATLAB®defaults to a file with.mextension, 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 selectOpenscriptName为活的脚本from the context menu. Alternatively, go to theEditortab, clickSave, and selectSave As. Then, set theSave as type:toMATLAB Live Code Files (*.mlx)and clickSave.

Note

You must use one of the described conversion methods to convert your script to a live script. Simply renaming the script with a.mlxextension 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); plot(r) m = mean(r); hold on plot([0,n],[m,m]) hold off title('Mean of Random Uniform Data')

现场编辑器中的新现场脚本,其代码绘制了随机数据及其平均值的向量

Run Code

To run the code, click the vertical striped bar to the left of the code. Alternatively, go to theLive Editortab and clickRun. While your program is running, a status indicatorappears 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 iconto 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.mlxextension. For example, go theLive Editortab, clickSave, and enter the nameplotRand. MATLAB saves the live script asplotRand.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, pressCtrl+Shift+O. OnmacOS, pressOption+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 selectDisable 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 selectClear Output. To clear all output, right-click anywhere in the script and selectClear All Output. Alternatively, go to theViewtab and in theOutputsection, click theClear all Outputbutton.

To open individual outputs, such as variables and figures, in a separate window, click the Open in figure window buttonin 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 theFiguretoolstrip. You can use the tools to explore the data in a figure and add formatting and annotations. For more information, seeModify 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 buttonto the right of the live script. You also can go to theViewtab and in theViewsection, selectOutput 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 buttonto the right of the live script or in theViewtab. 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 theHometab, in theEnvironmentsection, clickPreferences. SelectMATLAB>Editor / Debugger>Display, and then select a different option for the住编辑默认视图.

Format Text

您可以在实时脚本中添加格式的文本,超链接,图像和方程式,以创建一个可与他人共享的可观文档。例如,将标题和一些介绍性文本添加到plotRand.mlx:

  1. Place your cursor at the top of the live script and, in theLive Editortab, selectText. A new text line appears above the code.

  2. Click the Select Style buttonand selectTitle.

  3. Add the textPlot Random Data.

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

  5. PressEnterto move to the next line.

  6. Type the textThis 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现场编辑器中的格式文本.

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 theCtrl + Plus (+)andCtrl + Minus (-)keyboard shortcuts or theCtrl + Mouse Scrollkeyboard shortcut. OnmacOSsystems, use theCommandkey instead of theCtrlkey.

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

将实时脚本保存为普通代码

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

  1. On theLive Editortab, in theFilesection, selectSave>Save As....

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

  3. ClickSave.

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

Related Topics