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.

Â