Main Content

Create and Run Sections in Code

Since R2021b. ReplacesCode Sections(R2021a) andRun Sections in Live Scripts(R2021a).

MATLAB®code files often contain many commands and lines of text. You typically focus your efforts on a single part of your code at a time, working with the code and related text in pieces. For easier document management and navigation, divide your file into sections. Then, you can run the code in an individual section and navigate between sections, as needed.

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

To create a section, go to theEditororLive Editortab and in theSectionsection, click theSection Breakbutton. 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 (%%). The text on the same line as%%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.

File open in the Editor showing two percent signs on line eight and a blue border above line eight indicating the start of the section

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. A stale section is a section that has not yet been run, or that has been modified since it was last run.

File open in the Live Editor showing a blank code line on line eight and a blue border above line eight indicating the start of the section

删除Sections

To delete a section break in the Editor, delete the two percent signs (%%) at the beginning of the section. To delete a section break in the Live Editor, place your cursor at the beginning of the line directly after the section break and pressBackspace. Alternatively, you can place your cursor at the end of the line directly before the section break and press the删除key.

Note

You cannot remove sections breaks added by MATLAB. For more information about when MATLAB might add a section break, seeBehavior of Sections in FunctionsandBehavior of Sections in Loops and Conditional Statements.

Run Sections

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.

This table describes different ways to run your code.

Operation Instructions
Run all the code in the file.

On theEditororLive Editortab, in theRunsection, clickRun.

Run the code in the selected section.

On theEditororLive Editortab, in theSectionsection, clickRun Section.

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

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

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

On theEditororLive Editortab, in theSectionsection, selectRun and Advance.

在选择部分运行代码,然后运行all the code after the selected section.

On theEditororLive Editortab, in theSectionsection, selectRun to End.

Run to a specific line of code and pause.

Click theRun to Here button to the left of the line. If the selected line cannot be reached, MATLAB continues running until the end of the file is reached or a breakpoint is encountered.

In the Editor, theRun to Here button is available only for code that has been saved. In the Live Editor, theRun to Here button is available for all code, whether it is saved or not. In functions and classes, theRun to Here button is available only when evaluation is paused.

For more information, seeDebug MATLAB Code Files.

Increment Values in Sections

You can increment numeric values within a section, rerunning that section after every change. This helps you fine-tune and experiment with your code.

To increment a numeric value within a section, use controls in the Live Editor. For example, this code calculates the factorial of the variablex.

x = 5; y = factorial(x)
y = 120
To interactively change the value ofx, in a live script, replace the value5with a numeric slider. By default, MATLAB reruns the current section when the value of the slider changes.

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, seeAdd Interactive Controls to a Live Script.

Navigate Between Sections

You can navigate between sections in a file without running the code within those sections. This navigation facilitates jumping quickly from section to section within a file. You might navigate this way, for example, to find specific code in a large file.

Operation Instructions
Move to specific section.

On theEditororLive Editortab, in theNavigatesection, clickGo To. Then, in theSectionssection, select from the available options.

Move to previous section.

On theEditororLive Editortab, in theNavigatesection, clickGo To, and then clickPrevious Section. Alternatively, you can use theCtrl+Upkeyboard shortcut.

Move to next section

On theEditororLive Editortab, in theNavigatesection, clickGo To, and then clickNext Section. Alternatively, you can use theCtrl+Downkeyboard shortcut.

Behavior of Sections in Functions

In the Editor, if you add a section break within a function, MATLAB inserts section breaks at the function declaration and at the function end statement. If you do not end the function with anendstatement, MATLAB behaves as if the end of the function occurs immediately before the start of the next function.

In the Live Editor, you cannot add section breaks 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.

Behavior of Sections in Loops and Conditional Statements

In the Editor, if you add a section break within a loop or conditional statement (such as anifstatement orfor循环),行诈骗MATLAB添加部分减免taining 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 x (n (n) = 2 *- 1); if rem(x(n), 2) == 0 x(n) = x(n) + 1; end end

If you add a section break at line 3, inside theforloop, MATLAB adds a section break at line 9, the end statement for theforloop. If you add a section break at line 6, inside theifstatement, MATLAB adds a section break at line 8, the end statement for theifstatement, leading to three levels of nested sections.

  • At the outermost level of nesting, one section spans the entire file.

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

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

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

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

Related Topics