class: center, middle, inverse, title-slide .title[ # Scripts ] .subtitle[ ## Organising our code in scripts ] .author[ ### Lincoln Colling ] --- <!-- vim: set ft=markdown tw=80 spell spelllang=en_gb: vim: set conceallevel=0 foldlevel=1: --> # Scripts Up to now, we've been typing our commands at the `Matlab` command prompt, `>>` in the **Command Window**. But this isn't very efficient because often we'll want to re-use code, or save code for later use… We can use **scripts** (files with an `.m` extension) to store a series of commands that we can then re-run. --- ## <small>The search path</small> Before we can start using scripts in `Matlab`, we have to understand the concept of the **search path**. The **search path** is a list of all the folders on your computer where `Matlab` will look for `.m` files. By default, the search path will include several folders related to `Matlab` and a folder in your `Document` folder, usually something like: - `/Users/USERNAME/Documents/Matlab` or - `C:/Users/USERNAME/Documents/Matlab` You can *view* your search path with the `path` command: ```octave >> path MATLABPATH /Users/lc663/Documents/MATLAB /Users/lc663/matlab /Applications/MATLAB_R2019b.app/toolbox/matlab/timefun ``` --- ## <small>The search path</small> .center[<img src="path_annotated.png" width="95%" />] My *poorly* annotated `Matlab` window showing an example of a folder in the search path, a folder not in the search path, and the current folder… --- ## Our first script The easiest way to create a new script (or function) file is with the `edit` command. Type `edit` followed by the name of the new file you want to create. ```matlab >> edit say_hello.m ``` Valid names for script files: 1. Must be **one** word without spaces. 2. Has the extension `.m` 3. Doesn't start on a number Helpfully, `Matlab` will show an error and give you a chance to edit the name if you don't follow the rules! --- ## Our first script We can create our first script called `say_hello.m`<sup>1</sup> You can copy the code below into the editor window and save it! ```matlab % A simple script name = input('What is your name? ','s'); % ask your name disp(['Hello ' name '! Pleased to meet you']); % say hello ``` Once we've saved the script, we can run it by typing its name at the command prompt. ```matlab >> say_hello ``` Or by using the `run` command with the filename (this is useful if the file is **not** in our search path) ```matlab >> run say_hello.m ``` .footnote[<sup>1</sup>You can also cut and paste the code for this script from the [Part 1](/part1.html) page] --- ### Script sections Our simple script has one section, but it can be useful to break scripts down into sections. Sections can be run *independently*, which can be useful when we're debugging code. To create a script section, do two comment symbols `%` on a line by themselves. ```matlab disp('I am section 1') %% disp('I am section 2') ``` You can run sections by clicking on the **Run Section** button on the toolbar, or by hitting CTRL+ENTER / CMD+ENTER when your cursor is in a section. --- ## Adding help to a script Back in Part I, we saw that we could get **help** on `Matlab` commands by using the `help` command. For example, to get help on the `randn` function: ```matlab >> help randn ``` The same works for scripts/functions you write yourself: ```matlab >> help say_hello A simple script ``` Any comments at the start of the file are printed out when you ask for `help`. A good habit is to write your own documentation (what gets printed out by `help`) for any of the scripts and functions you write.