In this section we’ll start to work with scripts and functions. We’ll also cover variable scope, logic, and flow control.
In this section, we’re going to learn about how to keep our code organised in scripts
Let’s create our first script. We’ll use the code in the snippet above.
There are a couple of ways to go about creating our first script. We could download the file above making sure it save it in our search path, or we could directly create a new file from the Matlab
command prompt. Let’s try the second approach.
Try typing the follow at the command prompt
This should open a new file called say_hello.m
in the editor window. Cut and paste the code for our first script into this file and save it.
You should now have a script file called say_hello.m
. Make sure that you can run the script by typing the name at the command prompt.
After you’ve made sure that you can run the script by typing the name, see if you can get help using help
.
Next, we’ll try download a script. Click the download link below the following code snippet and save the file somewhere in your search path.
If you’re not sure of your search path, then try using the path
command to list all the folders in your search path.
Now let’s try modify our search path. Create a new folder somewhere not in your search path. Let’s call the folder experiments
Download the code above and save it to your experiments
folder. It doesn’t matter if you don’t understand the code, because it includes some concepts we haven’t covered yet.
Now add the folder to your search path using the addpath
function. For me, I’d write something like this:
Now see if you can run it by typing the script name at the command prompt.
In this section, we’re going to learn how to make reusable code by writing functions.
We’ve create a few scripts. Now let’s create a function. Create new file and copy the follow code into it.
Remember, the file should have the same name as the function.
Now I’d like you to try write a function on your own. Here are the specifications:
The function should take in a vector of numbers
And it output the following things:
You don’t have to write it from scratch. See if you can modify one of the examples from the slides.
In this section we’ll learn how to use the flow control create programs that branch according to certain conditions, and we’ll learn how to use loops to run bits of code over and over.
In the first problem we’ll write a function, and we’ll add a little flow control. Here’s the specifications of the function.
The function should take in two vectors of numbers
Next, it should work out the pair-wise difference between then (i.e., the difference between element 1 in vector 1 and element 1 in vector 2, the difference between element 2 in vector 1 and element 2 in vector 2 etc)
Next it should perform a one sample t-test on the differences (check out the help for the ttest
function)
If the difference between the numbers is significant, then it should display a message that says There is a significant difference between x and y
and if the difference is not significant then it should display a message that says There is NOT a significant difference between x and y
(check out the help for the disp
function, or look at the examples above)
Finally, if the difference is significant then it should output a 1 and if it isn’t the it should output a 0
Once you’ve written your function, try it out on these numbers:
Remember the berry_broadbent.m
script from above. You should now know enough to understand most of it. Read over it again, and see if makes more sense.
Now let’s play with some loops.
First let’s create some dummy data and store it in a struct array called people
This will create a variable called people
in your workspace.
If you type people
at the command prompt then you’ll see the following:
You can index this type of data structure as follows:
Write some code (either a script or a function) that loops through people
and prints out the name of any person that has a pet!