2.3. MATLAB Scripts

Scripts are files with a .m file name extension containing the same type of commands that one might enter in the Command Window. Scripts simplify the task of developing a correct program because they allow code to be saved, modified, and run repeatedly; whereas, to re-run code typed into the Command Window, the code would need to be retyped with any needed corrections.

You will see in the development environment that MATLAB also supports a new type of script file called a Live Script. Live scripts can be more interactive with output optionally displayed inline within the code. The code examples in this book may be used in traditional or live scripts. MathWorks has considerable documentation and demonstration videos about the use of live scripts.

Here are a few important points to know about script files.

  • 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, select either New Script, or New -> Script.

  • It is possible to run 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 (%) begins 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 click on run section in the editor tab ribbon.

2.3.1. Displaying Results

As you read MATLAB code, you will quickly notice that sometimes a semicolon (;) is placed at the 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 is too much to display on the screen, or you just don’t want to see it, add a semicolon after the command.

  • Many commands, such as plotting commands, do not produce output to the Command Window, so there is no difference between using a semicolon or not.

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

>> b
b =
    5

In a script, you may want to use disp or fprintf to show results. We discuss them in more detail in Output Functions. You may also enter a variable name in a script without a semicolon to see its value, but MATLAB will display a warning telling you that you should terminate a statement with a semicolon in a script.

>> disp(b)
    5

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

2.3.2. Adding Sections

You can divide a script into sections. Sections add structure to the script and make developing code that accomplishes a specific task easier because you can modify and run the code in just that section until it is correct.

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

%% Title of a Section

% The code of the sections goes here.

%% The start of a new 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. Comments can appear on lines by themselves, or they can be appended to the end of a line.

The first contiguous block of comments after a function declaration ( MATLAB Functions) is reserved for help information displayed when you use the help or doc commands. Comments after the first blank line are not part of the help documentation.