0:09

We are now at the fourth lecture

on Computing Using the MATLAB Programming Environment.

After this we're going to start actually implementing

some models, and discussing some dynamical mathematical models.

This is going to be our last lecture specifically focused on how to use MATLAB.

0:28

So what we're going to discuss in this final lecture on MATLAB

is the difference between scripts and

functions in the MATLAB programming environment.

And that's going to require us to discuss differences between what

we call local variables, versus what we call global variables.

0:52

What if we acquired fluorescence data that had

a structure like what we see down here.

We had a file called sampledata1.DAT And this file had, had a structure like this.

It had four columns, where the first column represented

time points, the second column represented fluorescents we acquired

in the blue channel, the third column fluorescents from

the red channel, fourth column fluorescents from the green channel.

And I just illustrated the first 12 or so data points

here, but we had hundreds more data points in this file.

1:31

Well, we could write a sample script that looked like this.

And because this is a sample, I'm calling it samplescript.m.

The first line would be to read in the data.

And we le-, we learned, learned about this command i-, in the previous lecture.

We would say data equals dlmread sampledata1.DAT.

So what we said in the previous slide was that this the structure of this .DAT file

was delimited text, so this dlmread command would, would handle it.

It would take the numbers that we had in this file,

sampledata1.DAT, and put them into an array called, that we're calling data.

2:07

And then, because of the structure we just discussed, we would say time is equal to

data:, one first column of data, f1 for fluorescence channel

is the second column of data, f2 is the third column, f3 Fourth column.

Then we would create a figure, and then we would want to say, hold on,

because we want to plot these three channels

of Foresis data on top of one another.

And we would plot time versus f1 first, then would make that blue.

Time versus f2, make that green.

Time versus f3 make that red.

2:49

where we would, if we typed: clear all, and then, remember that if you

have a script, and you save it, and the, the name of that script.m.

You could type that file name, type: samplescipt in the MATLAB Cmd window.

And it would generate a plot that looks like this,

and if you type whos afterwards, you would see, we have

3:09

an array called data which had 4,501 rows, four columns, f1, f2, f3,

and time each have 4,501 rows and one column and.

What we would see on our screen would look like this, where the red channel would

be up here, the blue channel would be

here, and the green channel would be down here.

But now let's assume for the sake of argument, that these

absolute values of florescence in this case, aren't really so important.

What's more important are the relative changes.

And this is actually typical of

an experiment where you might acquire florescence.

Because the absolute number you get here for flo-, florescence might

depend on something like the gain that you set on your microscope.

And what's really important is that you

have this time court, this time change here.

This green channel you can see goes up and goes down.

So what we want to be able to plot here are the relative changes in florescents.

4:17

And we'd say time would be the first column, F1 would be the second column.

But now what we want to do is we want to

normalize to whatever the average value is in the first 50 milliseconds.

So we would have to say the, first, we'd

have to say, where do we cross 50 milliseconds?

So, we'd say the minimum.

And then the, the index is the minimum of the absolute value of time minus 50.

This is figuring out where, in our time vector, are we closest to 50 milliseconds.

And then, what we want to do, is, we want

to determine the average value in this first, first 50 milliseconds.

Well, the first 50 milliseconds is from the first element up to the element index.

So, then we would create a For loop that would say, for

i equals one to index, we want to sum, keep a running sum.

We want to take whatever the value is of florescence

there, and add it to the previous value of sum.

5:05

And then when we're done, going up to the, all the numbers up to index, we

would say, the average value of f1 is equal to the sum divided by the index.

And now what we want to do is create a normalized value of

our fluorescence f1, dividing it by this f1 average where f1 average is.

The average value that we determined over the first 50 milliseconds of F one.

5:29

So, we could do that to plot,

to, to normalize channel, florescence channel one.

Right?

And then, we could say F two is the third column of data.

We could copy and paste these same lines to

average, to compute the sum, where the first 50 milliseconds.

Then compute the average by taking this sum divided by the number of points

you have in the first few milliseconds,

and then normalize this trace by the average.

And then we can do the same thing for f3 which is at the fourth column of the data.

And then finally we would we would say figure

to create a new figure, hold on and plot

time versus the normalized version of f1, the normalized

version of f2, and the normalized version of f3.

And now we would get traces that look like this.

And what you can see in this case is that the this was

not apparent when we plotted the, the raw traces so much, but you

can see that the green trace doesn't go up and just go down,

it goes up and goes down but then it has this baseline decay.

And in this particular case the right channel that we're

recording here is just there to monitor the baseline of decay.

Because you can see the timecourse of the red decaying matches the time course of

the green channel of decaying, which in this case is is due to photo bleaching.

So this is how we could, modify our sample script in

order to, to in order to normalize each one of our traces.

But it gets a little tedious to take all these lines of, of MATLAB code

and do this for the first channel, and the second channel, and the third channel.

And if we had 50 channels of data, then

we would be copying and pasting it every single time.

But if we have a bunch of commands that we're doing over

and over and over again There's a simpler way to do that.

7:10

And, if we knew that we were going to be, doing

the same sorts of commands over and over and over again,

it's often very convenient to create a function, that consists

of those commands that we want to do over and over again.

And this is an example of how we would do that.

7:24

We would have our script called sample script.m and then we'd have a second file.

Called normalize.m.

In this file normalize.m, would be a function.

So let's look at what, how we would modify samplescript.m

in this case, and then we'll look at the function.

7:39

We would still say data is dlmread, sampledata1.DAT.

We'd still set time equal to the first column of data.

We still determine the index, the min index value

for where we, time reaches 50 milliseconds same way.

But now we would just say F1 underscore norm equals normalize F1 comma index.

Similarly F2 is normalize F2 index and F3 is normalize F3

index, so you can see the samplescript.m got simplified quite a bit.

It got a lot cleaner in this case.

The way that we were able to do this is by taking, the

commands that we repeated, and putting them

in this function called normalize dot M.

So, the way that we defined our function, normalized dot

M, is the very first word in this is function.

This is a special key word in MATLAB.

You might say,' This is not just a regular script.'

This is not just a bunch of lines of MATLAB text.

This is something special.

This is a function.

8:35

And now what we are defining here is norm, we say norm equals normalize parentheses.

And I put a dot dot dot here to indicate here

to indicate that this is extending out to the next line.

Vector, and num points.

What is this defining here?

Norm is defining what the result is, this function normalize.

8:55

Normalize here is the name of the function.

So this word here that you have on this side of the equals sign has to

be the same as what you have with the file name that you save it to is.

So normalize here, and normalize.m are the same, and that's not by accident.

9:10

And then you have vector and numpoints.

These are the two variables that get sent to normalize.m.

So here when we called normalize, we said send it F1 and send it index.

You're saying send it a vector and send it

a number to indicate how many points I want to normalize.

And over here, when you look at normalize, you can see, this is a vector that's going

to be normalized, and these are the number

of points that you're going to use for the normalization.

Now these lines of MATLAB code are the same as what we had before.

First, we compute the the sum of all the numbers,

of all the elements of our vector, one to num points.

Then we compute the average.

It's the sum divided by the numpoints, and then we take our vector

and we divide by the average in order to get a normalized vector.

We're calling, norm in this case.

And then, to signify that we're finished with this

function, we say we use the term return here.

So if we had a, a function called samplescript.m.

And so if we had, had a script called samplescript.m and

a function called normalize.m and if we type sample script at

the MATLAB command prompt we would get the same answer that

we got before when everything was in the script called samplescript.

10:25

So this is a way to illustrate

the schematic relationship between scripts and functions.

We can say result equals samplefunction A,B,C.

It's taking these three arrays, A, B and C, and ascending them to samplefunction.m.

And because we're saying that after we call samplefunction,

whatever the, whatever happens with samplefunction is going to

be equal to result Samplefunction.m is going to send

back something that we're then going to call result.

10:54

And then within function it's going to look like this.

It's going to say function a equals sample function x, y, z.

Then there's a bunch of MATLAB code here that performs your computations.

And then your last line is usually going to be something that like.

A equals, some, some calculation here so, your function is going to send

back a variable that is, that the function is calling a, and

so therefore it makes sense that your last line in your function

right before the return, is going to, be the final computation, of a.

Now you may be noticing something here that's that is often confuses people.

11:32

Sample function, when you're calling it, it's sending it three

variables that we're calling capital A, capital B, and capital C.

When they get to sample function, they're going to be called something different.

They're going to be called x, y, and z.

So you're reading that correctly.

The things that are called a, b, and c in

the script are called x, y, and z in the function.

But this is okay.

This is actually a good thing, rather than a bad thing.

And then similarly we're calling this variable little A

within the sample function and then when it gets sent

back to the script it's going to be called result

once we're in the script, but again this is okay.

12:09

Something else to know about functions is that after the function gets called.

All the variables that are defined within the function are gone.

So it's performing these computations here and it's calling these variables little

x, little, y, little z and little a, but then once it's,

once this one is done and you go back to the script

12:35

Now you may be saying to yourself but what if I need to

use exactly the same variables in both the script and in end function?

And there's a way to do that.

And it's to declare for these to be

what we call global variables instead of local variables.

12:51

So, to give you an example of how you might define global variables.

Let's consider this variable, index.

Right?

In samplescript.m, what does "index" mean?

We said minimum comma index equals the minimum absolute value of time minus 50.

So index is telling us what value in our, what index number

in our data, what point in our data is closest to 50 milliseconds.

Because everything up to 50 milliseconds we want

to average in order to perform our normalization.

So remember that index is going to be used to normalize f1 and it's

going to be used to normalize f2 and it's going to be normalized f3.

So we can declare this to be a global variable, and that's what I've done here.

13:34

And so I've, I've modified samplescript.m, and

I've modified the function normalize in order

to utilize global variables, and now index

is going to be defined as a global variable.

13:45

So to, so to better illustrate the differences I've sort of taken the I've

taken the lines that have been changed and I've put them in red font here.

So you can see a little bit more clearly what what

parts of this, this MATLAB code has been, has been modified.

So one thing we said here in samplescript.m

is we said index is going to be global variable.

14:05

That's necessary.

And you can see than index is defined as a global variable in samplescript.m.

And index is also defined as a global variable within the function.

And that's really important.

You have to define a variable as global.

Both, within the script that calls the function.

And then also within the function that's going to use this global variable.

14:25

Something else that's changed, is, when, when we used to call normalize,

we sent in a vector, and we also send up the index.

Now, you don't need to have the index be,

something to get sent to the, to the function.

So, you can see here, normalize is only expecting one variable.

And here, when we're calling normalize, we're only sending

it one variable instead of sending it two variables before.

Because index is going to be a global variable, we

don't need to send it to the function anymore.

14:54

So, when we call normalize each time, that's been modified.

And then the third thing here is, in

the previous version of this function, we called

it, this variable," num points" for the number

of points to average, when we called normalized.

But now we're going to use this global index, so now we're looped

here instead of going up to num points, it goes up to index.

And then we, when we compute the average instead

of dividing by num points, we're dividing by index.

So this is how we can do this samplescript.m, and do normalize here with

using our variable index it's a global variable rather then as a local variable.

To summarize, a script is

a series of MATLAB commands that are all saved to a file with a dot" M" extension.

And this can be executed by typing the file name at the MATLAB command window.

So in the example we showed, we saved it as, samplescript.m," then

when you get to the MATLAB command window and you type, samplescript.

It executes all the MATLAB commands within that script.

16:00

A function must be divides, must be defined as such.

In other words, the very first word in a function,

is the word function which indicates yes, this is a function.

This is different from just a bunch of MATLAB code, it's different from a script.

16:31

So, in our, one of our examples, we had some variables called little

x, little y, little z, and little a, that we're defined within the function.

Once the function was over, those variables were gone.