MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Code Sections


In this section...
“Divide Your File into Code Sections” on page 18-7
“Evaluate Code Sections” on page 18-7
“Navigate Among Code Sections in a File” on page 18-9
“Example of Evaluating Code Sections” on page 18-9
“Change the Appearance of Code Sections” on page 18-12
“Use Code Sections with Control Statements and Functions” on page 18-13

Divide Your File into Code Sections


MATLAB files often consist of many commands. You typically focus efforts on a single part
of your program at a time, working with the code in chunks. Similarly, when explaining
your files to others, often you describe your program in chunks. To facilitate these
processes, use code sections, also known as code cells or cell mode. A code section
contains contiguous lines of code that you want to evaluate as a group in a MATLAB
script, beginning with two comment characters (%%).

To define code section boundaries explicitly, insert section breaks using these methods:


  • On the Editor tab, in the Edit section, in the Insert button group, click.

  • Enter two percent signs (%%) at the start of the line where you want to begin the new
    code section.


The text on the same line as %% is called the section title. Including section titles is
optional, however, it improves the readability of the file and appears as a heading if you
publish your code.

Evaluate Code Sections


As you develop a MATLAB file, you can use the Editor section features to evaluate the file
section-by-section. This method helps you to experiment with and fine-tune your program.
You can navigate among sections, and evaluate each section individually. To evaluate a
section, it must contain all the values it requires, or the values must exist in the MATLAB
workspace.

Code Sections
Free download pdf