Part 2

In this section we’ll start to work with scripts and functions. We’ll also cover variable scope, logic, and flow control.

true

Scripts

In this section, we’re going to learn about how to keep our code organised in scripts


Ex

Download say_hello.m

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.

Ex

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.

Ex

After you’ve made sure that you can run the script by typing the name, see if you can get help using help.

Ex

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.

Download second_script.m


If you’re not sure of your search path, then try using the path command to list all the folders in your search path.

Ex

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 berry_broadbent.m


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.

Functions

In this section, we’re going to learn how to make reusable code by writing functions.

Ex

We’ve create a few scripts. Now let’s create a function. Create new file and copy the follow code into it.

Download say_hello_fun.m


Remember, the file should have the same name as the function.

Ex

Now I’d like you to try write a function on your own. Here are the specifications:

You don’t have to write it from scratch. See if you can modify one of the examples from the slides.

Flow control and loops

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.

Ex

In the first problem we’ll write a function, and we’ll add a little flow control. Here’s the specifications of the function.

Once you’ve written your function, try it out on these numbers:

Download numbers.m

Ex

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.

Ex

Now let’s play with some loops.

First let’s create some dummy data and store it in a struct array called people

Download dummy_data.m

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!