Main Content

Create and Run Sections in Code

自R2021b以来。替换代码部分(R2021a) and在实时脚本中运行部分(R2021A)。

MATLAB®代码文件通常包含许多命令和文本行。通常,您会一次将精力集中在代码的单个部分上,并将代码和相关文本组成。为了更轻松的文档管理和导航,请将您的文件分为部分。然后,您可以根据需要在各个部分中运行代码并在各节之间导航。

File open in the Editor showing two sections. The second section has a blue border around it indicating that it is the selected section.

Divide Your File into Sections

要创建一个部分,请转到Editor或者现场编辑标签和部分部分,单击the部分中断button. You also can enter two percent signs (%%) at the start of the line where you want to begin the new section. The new section is highlighted with a blue border, indicating that it is selected. If there is only one section in your code file, the section is not highlighted, as it is always selected.

In the Editor, a section begins with two percent signs (%%)。与同一行的文字%%is called thesection title。Including section titles is optional, however, it improves the readability of the file and appears as a heading if you publish your code.

在编辑器中打开文件,在第八行上显示了2%的符号和第8行上方的蓝色边框,指示本节的开始

In the Live Editor, a section can consist of code, text, and output. When you create a section or modify an existing section, the bar on the left side of the section is displayed with vertical striping. The striping indicates that the section isstale。陈旧的部分是尚未运行的部分,或自上次运行以来已被修改。

在实时编辑器中打开文件,在第8行上显示空白代码行,第八行上方的蓝色边框表示该部分的开始

删除部分

要删除编辑器中的部分中断,请删除2%的符号(%%)在本节的开头。要删除Live编辑器中的部分中断,请将光标直接放置在截面断裂后的线路开头,然后按backspace。Alternatively, you can place your cursor at the end of the line directly before the section break and press the删除key.

笔记

您无法删除MATLAB添加的部分中断。有关MATLAB何时可能添加部分中断的更多信息,请参阅Behavior of Sections in Functions循环中部分的行为和条件语句

最小化部分边距

为了最大化编辑器中可用的编辑代码的空间,您可以隐藏运行部分,运行到此处以及代码折叠保证金。这将代码左侧的灰色区域最小化。要隐藏一个或多个边距,请右键单击代码左侧的灰色区域,然后清除Show Run Section Margin,,,,表演跑到这里的边距,和/或显示代码折叠边距选项。

跑部分

You can run your code file by either running each section individually or by running all of the code in the file at once. To run a section individually, it must contain all the values it requires, or the values must exist in the MATLAB workspace. When running individual sections, MATLAB does not save your file and the file does not have to be on your search path.

该表描述了运行代码的不同方法。

Operation Instructions
运行文件中的所有代码。

Editor或者现场编辑tab, in the部分,单击

在选定部分中运行代码。

Editor或者现场编辑tab, in the部分部分,单击跑部分

In the Live Editor, you also can click the blue bar to the left of the section.

部分in the Live Editor showing a blue bar to the left of the section.

跑the code in the selected section, and then move to the next section.

Editor或者现场编辑tab, in the部分部分,选择跑步并进步

在选定部分中运行代码,然后在选定部分之后运行所有代码。

Editor或者现场编辑tab, in the部分部分,选择运行到结束

运行到特定的代码和暂停行。

Click the运行到行左侧的按钮。如果无法达到所选的行,则MATLAB继续运行,直到到达文件末尾或遇到断点为止。

在编辑中,运行到此处的按钮仅适用于已保存的代码。在现场编辑中,跑to Here button is available for all code, whether it is saved or not. In functions and classes, the仅当评估暂停时,运行到此处只能使用。

For more information, seeDebug MATLAB Code Files

章节中的增量值

您可以在一节中递增数字值,每次更改后都会重新整理该部分。这可以帮助您微调并尝试您的代码。

要在一节中增加数字值,请在实时编辑器中使用控件。例如,此代码计算变量的阶乘X

x = 5;y =阶乘(x)
y = 120
To interactively change the value ofX,在实时脚本中,替换值5带有数字滑块。默认情况下,当滑块的值更改时,MATLAB重新运行当前部分。

Code that calculates the factorial of x. The value of x is replaced with a numeric slider with a minimum value of 0, a maximum value of 10, and an actual value of 5.

For more information, see将交互式控件添加到实时脚本

导航Between Sections

您可以在文件中的各节之间导航,而无需在这些部分中运行代码。该导航有助于在文件中从截面迅速跳跃。例如,您可以通过这种方式导航,以在大文件中找到特定代码。

Operation Instructions
Move to specific section.

Editor或者现场编辑tab, in the导航部分,单击。Then, in the部分部分,选择from the available options.

移至上一节。

Editor或者现场编辑tab, in the导航部分,单击,,,,和then clickPrevious Section。或者,您可以使用Ctrl+Up键盘快捷键。

Move to next section

Editor或者现场编辑tab, in the导航部分,单击,,,,和then click下一节。或者,您可以使用ctrl+down键盘快捷键。

Behavior of Sections in Functions

在编辑器中,如果您在函数中添加部分中断,则MATLAB插入部分在函数声明和函数端语句中断开。如果您没有用一个结尾statement, MATLAB behaves as if the end of the function occurs immediately before the start of the next function.

在生活的编辑器中,您不能添加部分减免inside a function. Sections inside local functions are not supported. If you add local functions to a live script, MATLAB adds a section break before the first local function definition and removes all section breaks after it. When running individual sections in a live script, you can run only the sections that are before the local function definitions.

循环中部分的行为和条件语句

在编辑器中,如果您在循环或条件语句中添加部分中断(例如ifstatement or为了loop), MATLAB adds section breaks at the lines containing the start and end of the statement (if those lines do not already contain a section break). The sections within the loop or conditional statement are independent from the sections in the remaining code and become nested inside the sections in the remaining code. Sections inside nested loop or conditional statements also become nested.

For example, this code preallocates a 10-element vector, and then calculates nine values. If a calculated value is even, MATLAB adds one to it.

x =一个(1,10);n = 2:10 x(n)= 2 * x(n -1);如果rem(x(n),2)== 0 x(n)= x(n) + 1;结尾

如果您在第3行中添加部分中断,则为了loop, MATLAB adds a section break at line 9, the end statement for the为了环形。如果您在第6行中添加部分中断,则if语句,MATLAB在第8行中添加了一个部分中断,if声明,导致三个层次的嵌套部分。

  • 在嵌套的最外层层,一个部分跨越整个文件。

    File open in the Editor with the selected section spanning the entire file, and section breaks at line three, six, eight, and nine

  • At the second level of nesting, a section exists within the为了环形。

    在编辑器中打开文件,所选部分从第三行到第九行,然后在第六行和八行中断。

  • At the third level of nesting, one section exists within theifstatement.

    在编辑器中打开文件,所选部分从第六行到第八行,并且第三行和九个部分断开

相关话题