1. Very Basics
This section shows you how to use Matlab and R. The examples in text will be based on Matlab, though the equivalent code for R are found at the end of sections.
It doesn’t matter whether you use R or Matlab for modelling; the logic you use in coding is the same and there are more commonalities than not. Ecologists often prefer to use R over Matlab because there is an entire network of packages** (CRAN) used in running statistics and specialised analyses. Plus R is free while Matlab is not. Engineers and physical scientists tend to find Matlab advantageous.
**Note: Packages are programs/algorithms which run a set of functions for specific analyses or methods. The functions used within a package may be user-created.
There are many sources online on the topic of R vs. Matlab so we won’t discuss that here. Skills from both are easily transferable and interchangeable — they are both mathematically-based languages.
The Matlab interface: Command window — Workspace — Script
Type this in the Command Window:
a = 3
^Let’s say a is the number of apple trees you have. a takes on a value of 3 in the Workspace.
Type ‘a’ and press Enter in the Command Window. Doing this the same as asking Matlab: “What is ‘a’ again? ”. It will check its memory and respond with a = 3.
You planted 2 more apple trees today. If you want to add 2 to a, type:
a = a + 2
^Which is the same as saying ‘a’ becomes ‘a + 2’. (a ← a + 2)
Now, if you want to actually save this stuff for later — or want to write something before ‘a = 3’, you’ll need to rewrite all this as a Script.
Click on ‘New Script’ on the bar above. Write:
^Click on “Run” on the bar above. It will ask you to name and save your file (filename.m). Note that file names and variable names cannot have spaces or strange symbols. You can use underscores in place of spaces.
If a window pops up click Add to path , then it will run the script.
In the Command Window you’ll see ‘a = 3’ and right under it, ‘a = 5’.
You don’t want to see that “a = 3” again. These outputs are called “echoing”. The past belongs to the past. You only want to see the end product ‘a’. Suppress that line by adding a semicolon after the“a = 3” in the script (i.e. a = 3 ;)
Run it again.
You might have already noticed a couple of red squiggly lines below the equal signs. Hover your mouse over these, and you’ll see Matlab is already asking you: “Did you want to hide outputs from these lines as well? I don’t want to create a mess in the command window!”
You can imagine if you’re running a script on the fish populations of the Boganville River with lots of variables (water flow rate, number of fish this year, number of newborns this year, nutrient levels, predator number, etc)… and you only want the number of fish next year (THE Output) from all these inputs. You certainly don’t want to crowd the Command Window with rubbish. Fortunately you can suppress everything except for the end product: “number of fish next year”.
The semicolon is great for keeping things nice and tidy;
It is also very important we start with a clean slate every time we run a script.
Type in the functions clear all (clears memory cache) and clc (clear screen) before the ‘a = 3’. If you run some other script later and it also has a variable named ‘a’ (e.g. number of accidents), you know the apple trees won’t interfere.
The basic idea is that we write equations in the Script window, and look for results in the Workspace and Command Window. And that’s it.
In the next section, we learn to write and plot functions.
R
We’ll use R-studio (a version of the R-program with a more user-friendly interface) which has a lot of similarity with Matlab.
- Depending on your version of R or the packages present (see package list on the right side). The acceptance of articulation may vary. For example, “<-” is used to represent “=” as a standard for R ( a <- 3 not “a = 3”). However, my current version of R accepts a = 3 also.
- When you run a script via the “Run” button, a difference to Matlab is that you’ll have to SELECT/HIGHLIGHT the part of the script you’d like to run. A way around this: Ctrl-Shift-Enter. There is a “re-run last selection” button.
- Use Ctrl-Shift-s to run the script, suppressing all lines. The semicolon “;” is used to separate statements only (the semicolon separates and suppresses in Matlab). In the console, R displays the line being run instead of a variable’s value.
- Unlike Matlab, saving is not necessary before running a script, so ‘run’ away!