Vous êtes sur la page 1sur 5

Scripts and the Base Workspace

Scripts can operate on the variables that are present in the base workspace before the script is
run. Similarly, the variables that are generated after the script is run are stored in the base
workspace.

3.3 Code selection

3.3.1 Introduction: Code Sections

The overall structure of most scripts can naturally be divided into different sections. This is
especially nice for larger files where you may just want to focus on one area, such as adjusting a
plot.

Sections allow you to modify and reevaluate the code in just that section to see the impact.

3.3.2 Adding and Running Code Sections

Adding Code Sections

You can create separate code sections by adding a section break.

On the Live Editor tab in the Toolstrip, in the Section section, click Section Break.

You can also add a section break by using the keyboard shortcut Ctrl+Shift+Enter.
Running Code Sections

You can run the code sections through the Section section of the Live Editor tab in the Toolstrip.

Run Section: Run code in the current section.


Run and Advance: Run code in the current section, then
move to the next section.
Run to End: Run code in the current section and all code
after it.
When you run a section using the “Run Section” button, all the sections following the selected
section are also executed.

Summary: Code Sections

Code sections allow you to organize your code and run sections of code independently.

On the Live Editor tab, in the Section section, click Section Break to create a new code section, or
press Ctrl+Alt+Enter.

You can use the controls on the Live Editor tab of the toolstrip to create, navigate to, and run
different sections.

3.4 Comments and Text:


3.4.1 Introduction: Comments and Text

Readability of your code file is


important if you want to quickly
scan your code to recall what it
does, or share it with a
colleague who isn't familiar
with it.
As the number of lines of code
in a script file increases, it
becomes harder to interpret
the code.

Adding text descriptions that


are not executed allows you to
explain the functionality of your
code.

3.4.2 Comments and Formatting

Formatted Text

You can add a line of text to a live script by clicking the Text button in the Text section of the Live
Editor tab in the MATLAB Toolstrip. You can then format the text using the formatting options
provided in the Text section.

Comments

To create a comment, add % comment where you want to add more information. Comments let
you annotate a continuous block of code without inserting lines of text. Comments can appear on
lines by themselves, or they can be appended to the end of a line.
load AuDeMx
% Converts from US$/gal to US$/L
gal2lit = 0.2642; % conversion factor
Germany = gal2lit*Germany;
Australia = gal2lit*Australia;
Mexico = gal2lit*Mexico;

3.5 Sharing Live Scripts:


Introduction: Sharing Live Scripts

You may want


to share your
code and
results with
your
colleagues. You
can share your
live scripts
directly with
other MATLAB
users, or you
can publish a
static
document that
includes your
code,
formatting,
links, and plots.

Exporting Live Script Files

You can export your live script and results


using the Save button in the Live Editor tab.
Available formats include PDF, HTML, and
LaTeX.
The saved file will closely resemble the live
script when viewed in the Live Editor with
output inline.
3.6 Project - Using Scripts:

Vous aimerez peut-être aussi