2.3. MATLAB Scripts

Reading Assignment

Please read chapter 2 of Physical Modeling in MATLAB, by Allen B. Downey [DOWNEY11].

  • Scripts are files, with a .m file name extension, containing the same type of commands that one might enter in the Command Window.
  • Scripts allow code to be saved, modified and run repeatedly.
  • Like the Command Window, scripts access variables from the global Workspace.
  • Create a script with the command edit myscript.m or from the Home tab, either New Script, or New -> Script.
  • It is possible to test just one or two lines of code at a time to verify the expected results.
    • Use the mouse to highlight a few lines of code, then press the F9 key on the keyboard to execute just those lines of code.
    • Another way to execute a few lines is to make those lines a section. The percent sign (%) is the beginning of a comment line. Two percent signs and a space character (%%) at the beginning of a line mark a section boundary. Select a section by clicking anywhere in the section and then, in the editor tab ribbon, click on run section.

2.3.1. Displaying Results

As you read about MATLAB, you will quickly notice that sometimes a semicolon (;) is placed at end of commands, but not always.

  • The semicolon suppresses output.
  • If you want to see the result of a calculation, leave off the semicolon. The output will be displayed in the Command Window.
  • If the output would be too much to display on the screen; or you just don’t want to see it, then add a semicolon after the command.
  • Many commands, such as plotting commands, do not produce output on the Command Window, so there is no difference between with or without the semicolon.

In the Command Window, just enter a variable name to see its value.

>> b
b =
    5

In a script, you may want to use disp or fprintf to show results.

>> disp(b)
    5

>> fprintf('The value of b = %d\n', b)
The value of b = 5

Read more about it

Look up the function fprintf in the MATLAB documentation.

2.3.2. Adding Sections

The overall structure of the scripts you create 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.

To create a code section, add %% Section Title at the beginning of a line. Note the space character between the second % and the Section Title text.

Tip

A optional window in the IDE called Panel Titles displays the section titles and allows a quick way to move to and select a section.

2.3.3. Comments

A few words of explanation can make it much easier to reuse a program after you have forgotten the details. Adding text descriptions that are not executed, called comments, allows you to explain the functionality of your code.

Text following a percent sign, %, is a comment. A comment is not executed in the code file. Comments can appear on lines by themselves, or they can be appended to the end of a line.

The first contiguous block of comments is reserved for help information which is displayed when you use the help or doc commands. Comments after the first blank line are not treated as part of the help.