hello and welcome back to another MATLAB video the objective of this video is to
show you how to create and run a basic MATLAB program so if we open up MATLAB
we'll see a big panel in the middle this is called the command window the command
window is used to execute commands one by one I'll show you what I mean we can
create a vector X that goes from 1 to 10 and when I hit enter this vector is
created in our workspace or I can create a vector Y which is equal to the square
root of x and when I hit enter this vector is also created or I can even
create a plot from these two vectors and when I hit enter this figure pops up on
the front of my screen so as we can see the command window is great if you want
to execute commands line by line but what if you want to execute multiple
commands at a time well you can do this by creating a live script let's close
out of this figure and I'll show you two ways to accomplish this the first way is
to click new and then live script and when you do this you'll see the live
editor window pop up on top of the command window here you can type
multiple commands without executing them right away
but let's say you already typed a bunch of code in the command window and you
want to preserve it for when you create your live script how do you do that
first go to your command history and highlight all the commands you want to
include in your script I'll pick the 3 that we just typed then right click
these commands and hit create live script you'll notice that just like
before the live editor pops up on top of the command window but this time it
already has the commands that we highlighted now to run this script I
first have to save my file by going to save save as and giving it a relevant
name then I'll go to the top here where it says run all and hit that button when
I do this all of the commands in the script are executed we can see in our
workspace that 2 vector X&Y were created and right next to our
code we see the figure that was generated from the plot command based on
your preference you can choose whether to have these outputs on the side of
your code or in line with your code these two buttons up here will let you
switch between the modes I'll switch to in line mode so you can see what it
looks like okay so the program we wrote so far is pretty simple I want to go
through a few tasks that you may use while writing something more complex
task number one is adding comments comments will help make your code
understandable typically they'll describe what your code is doing you can
add a comment by typing a percent sign and then typing your comment like this
another way to organize your code is to add sections to it what do I mean by
this well when using live scripts in MATLAB
you don't have to run the entire file you have the option to run your code
section by section sections will also visually split up your code making it
easier to read and comprehend to add a section I'll type two percent signs
followed by a space and the name of my section when I hit enter we'll see a
visual break in the code that separates the section I just created let's put a
couple of commands here to show you how to run a specific section of code I'll
create a vector X with values 1 to 100 and I'll create another vector Y with
values 101 to 200 and then I'll plot X comma Y and make the plot red now
instead of hitting run all like I did before
I'll highlight the section I want to run and hit run section we can see the
figure that corresponds to the section pop-up next to the code we just type and
if we click on one of these plot commands we can see that the
corresponding figure is highlighted this is a really useful feature for
navigating through your results now we've talked a little bit about
formatting our code but let's talk about some common tasks and constructs used in
programming the first thing that we'll talk about is called indexing indexing
is used to take a subset of or matrix that we already created for
example let's look at one of the vectors we created earlier why if we open the
variable editor we can see that each element in Y takes on a different value
now let's say we want to access the fifth element in this vector we can type
Y and then in parentheses five to get this number or if we want the seventh
element we'll type Y parentheses seven close parentheses here's some practical
examples of tasks that we can do with indexing we can create a new variable a
which is equal to the fifth element in Y a equals y of five now our workspace
shows that a is equal to the same value as Y of five or we can change the fifth
element of Y to be 100 we'll type Y of five equals 100 enter and we can see
that after executing this command the fifth value of Y has changed okay one
last example let's say we want to change the first five values of Y to be the
numbers six through ten we can type Y of 1 colon 5 equals and then in square
brackets 6 7 8 9 10 when we hit enter we can see that the first 5 values of Y
have changed accordingly so this covers some of the basics of
indexing and the next thing I want to talk about is something called the
if-else statement you'll use an if-else statement if you want to execute a set
of commands only if a certain expression is true this will be more clear with an
example let's start by writing a statement that takes in a user input the
line of code I just typed is going to ask for input from the user and convert
their answer to a string before proceeding I'll show you what I mean if
we run this section it asks us did you learn something in the command window
whatever we type as our answer will be stored in the variable user in as a
string now that we have our user input will create an if-else statement that
determines what to do based on that user's input so I'll type it all out and
then we can go through it starting from the top if the user types Y then the
first expression user in equals Y is true and will display hurray
but if the user does not type Y and types n instead then the second
expression is true and will display oh man finally if the user types anything
that's not Y or n will display please reread the directions
so let's actually run the program for the three cases
I'll hit run section and answer the question with Y for yes the live editor
executes the code and the if section and displays hurray I'll run it again and
this time I'll answer the question with n for no this time the live editor
executes the code in the else if statement and displays oh man I'll run
it one last time and I'll type something different as my answer now the live
editor executes the code in the else section and displays please reread the
directions now one last thing it's important to note that you can only have
one if and one else in each construct but you can have multiple elsif
statements for example I can add in the following section now if I run this
section and respond with Q the third expression is true and the live editor
displays why did you type Q the last construct that we'll talk about is
called loops specifically we're going to talk about four loops loops are used to
repeat a set of commands within your code so I have a function that will help
us illustrate when this comes in handy the function is called random walk and
I'll give you a visual explanation of how it works there's a person who's
standing a certain number of steps away from his home let's just call him LeBron
in the function LeBron flips a coin and if it
heads he takes one step towards his home and if it lands tails he takes one step
away from his home the question is how many coin flips will it take LeBron to
get back to his home so the input for the function is going to be his starting
position and when we run the function it automatically simulates his coin flips
until he reaches his house so the output for the function will be the number of
coin flips that it took him to get home for this example let's say LeBron always
starts 3 steps from his house so the input for the function will be 3 if I
hit run we can see that it took 15 coin flips to get back home let's run it a
couple more times and see what results we get okay notice how every time the
result is different and at this point you might be curious if we ran this a
hundred times what would be the average number of coin flips that it would take
LeBron to get home or maybe you're just wondering what the entire distribution
would look like instead of rewriting the same command 100 times we can create a
loop in this case a for loop so let me show you what I mean I'll type it out
and then I'll explain what's going on alright so what does all of this mean
well our first statement is for I equals 1 : 100 in this case I is called an
index variable it's responsible for counting the number of iterations we go
through in our loop so in this case we'll iterate through the loop 100 times
and I will take on values 1 2 3 4 5 6 etc all the way up until 100 everything
contained between the 4 and the end is going to be repeated 100 times in this
case our function is within our loop so this is what will be repeated
I'll walk us through a couple of iterations when we first enter the loop
I is equal to 1 and then we hit our statement num flips of I is equal to
random walk of 3 this is going to create a variable
called num flips remember that AI is equal to one so when we have I in
parentheses we are indexing into the first element of num flips we're going
to set that value equal to the output of random walk of three we reach the end of
our loop so we go back to the top and this time I is equal to two then we'll
hit our function again this time it will index into the second element of num
flips and set that value equal to the output of our function we hit the end of
the loop so we go back to the top and this time I is equal to three again will
execute the random walk function and this time the result is stored in the
third element of the vector we keep going through this loop until I is equal
to 100 and each time we go through it we add one more element to our vector
num flips so after 100 iterations we'll have a vector that has 100 elements in
it so let's go ahead and run this and take a look at the results in the
variable editor here's our vector with all 100 elements now let's do some basic
analysis one thing we might want to do is to check the mean of the data we'll
go back to the command window and use the mean command whoa that seems kind of
high you might be curious to see why it's so high so let's plot the number of
flips that it took on each trial just as a sanity check okay we can see that some
trials took thousands of flips until LeBron got back home so that's
definitely skewing our mean to the higher side since we have several large
outliers in our distribution the median may give a better understanding of the
data central tendency so let's type median of num flips and see what we get
okay that's a little bit closer to what we want so once we know what kind of
analysis we want to do we can then take those lines and add them to the live
editor and then go ahead and save the program
so that wraps up this video on creating a basic MATLAB program the examples in
this video we're pretty simple to illustrate the logic but these basic
principles are applicable for everything from analyzing big data all the way to
creating a graphical user interface and the best way to get familiar with
writing MATLAB programs is to practice if you get stuck there's plenty of
resources on MathWorks comm and you can post your questions in MATLAB answers as
well thanks for tuning in and I'll see you guys in another video