ABSTRACT

Although this is generally fine and useful to a certain extent,

in practice we are required to repeat and change commands.

Similarly, once a sequence of commands is able to perform

a determined task we are quite likely to use it again and

thus we need to store it. Not only does this simplify our

workflow but also helps in the reproducibility of results. All

these tasks can easily be achieved with the use of scripts

and programming directives. We shall talk about them in

Script files are simple text files that contain and

store MATLAB and Octave commands. These commands

are indeed those that we have described in the previous

chapters, with the added advantage that we can define logic

can files to

As mentioned above, script files are composed of unfor-

matted text and an easy way to distinguish them in our

file system is thanks to their extension: .m. For example, Scripts for MATLAB and Octave have the extension .m.we can in general tell that a file called MyScript.m is quite

possibly a script that can be executed by MATLAB and Oc-

tave. Thanks to the extension used, these files are commonly

known as m-files. The commands in any of these files can

be executed by typing the name of the file itself in the com-

mand line; the extension is not needed for the script to be

5.1.1 Text Editors

Running an m-file results in the commands contained

in the script to be executed and, in case it is required in the

programme, their output displayed on the screen. Scripts or

m-files can be created with your favourite editor as long as

they are saved as simple text. This is important as MATLAB Ensure that your scripts are saved as simple text.and Octave will read the script line-by-line and execute the

commands as they are encountered. If you use an editor

that adds formatting commands (such as Microsoft Word),

then the software will fail to run your script. In the case

of MATLAB, you can use the script editor that is included

with the software: simply click on the “New Document”

icon at the top left of the main MATLAB window. Once

you are in the editor, you just need to type the commands

needed and then save the file (remember that it should have

a .m extension). For Octave in a Macintosh environment a

number of users recommend editors such as Textwrangler,

Aquamacs or Sublime; whereas in windows Notepad++ is very useful. Finally, for Linux/Unix editors such as Emacs, Nano can

5.1.2 Adding Comments

It is a good practice to write comments that explain

what it is that you are trying to achieve with the flow of

your programme. This will help you, and anyone else using

your code, follow each bit of the programme and make

sense of the code. In order to create comments you use the

comment and is not executed by MATLAB and Octave. This

is very useful for debugging and trying different commands

in your scripts: since a commented line is not executed,

the % symbol can be used with good effect for telling the

software not to run one or more lines of code, without

having to delete them. Should you require them again later

Matlab and Octave use procedural programming

to execute the scripts. This means that whenever we run

a programme we invoke procedures, routines, methods or

functions that contain a series of computational steps to

be carried out one after the other. You can think of a script A script is executed sequentially, line by line.as a list of instructions that you are asking the software

to execute, and this is done on a line-by-line basis as we

mentioned earlier. In other words, the execution of the

m-file is done from the top to the bottom of the script

Although this is an easy procedure to understand, there

are times when we require the programme to follow a

repeat a certain procedure either a certain number of times

or until a condition is met. These decisions can be taken

with the help of Boolean operations that MATLAB and

Octave understand. In that way we can decide if something

is true or false, represented as 1 or 0, respectively, and take

5.2.1 Relational Operators

If at some point in a calculation a variable x has been

assigned a value, it is possible to make certain logical tests

on it: for instance we may check if the value held by x is

Please note that we are using two equal signs as this is a

logical test, not a value assignment, i.e. we are not assigning

line

comparisons to see if the value of the variable x is greater

than or less than a given number. Table 5.1 shows some of

the logical operators supported by MATLAB and Octave.