Creating Variables in Matlab

Sharing buttons:

All right, so we've got some MATLAB here. Yeah, MATLAB!

Here's some notes that you can use to follow along, and I'm gonna kind of play with these as well, but this is a MATLAB file.

Anything that has a dot M on it that's going to be considered a MATLAB file.

So, let's create our own MATLAB file.

You can either hit this new script, or you can come over here and create a new document.

Create a new script, but I'll just click here. You can create whatever you want.

Um, but if you save, it you can title it like my notes.

You can see, "my notes." and see, now I have a my notes file.

A script is basically the same thing as just typing directly into MATLAB.

So I can say x equals 5 and hit enter, and I see it goes x equals 5.

But then when if I'm like, "man I wish I could do that again! and that was really fun!"

I have to type x equals 5 again and "Wow that's a lot of work."

So, if I want it I can just type x equals 5 up here in my script and I can run my script.

And I could run it like as many times as I want, and it's like, "Wow! look how many times x equals 5!

That's awesome!"

So anyway that's that's the joy of script.

So, there's my script.

We can separate our script into sections by doing the double % sign.

So this is like my new section.

You don't have to call it your "new section" and I can call it, Bill... Hillbilly chicken head," or whatever.

I'll just call it, "new section" because that sounds that's kind of weird.

So, I can have my new section, and one of the nice things about,

especially taking notes in the sections, and in a script is that you can always like rerun the code,

that we ran in class, and see if you like it.

So, anyways, this is a script.

Um, anything that's just a percent sign by itself as a comment.

So, I'm like this is a comment, "x equals five",

and it can be like, I don't know this is another comment "X equals 6",

and then I can run it and I see "x equals five", and "x equals 6",

but it skips right over the this is a comment thing.

So, its a comment. It's how you can write stuff to yourself to help you remember what we did.

So, it starts with a %, you know, a new section starts with a double %,

Now, don't type everything that I type, because I type as fast as I think.

Well that's not true, I think faster than I type, but I can type pretty fast,

and so a lot of times, I type stuff just for fun to keep myself entertained.

Type things that are relevant to you.

If all you're doing for these lectures is typing exactly what I type

and not thinking about it you're totally wasting your time. So don't do that.

Ok, comments.

boop doop doop -

All right, so, let's look at some different rules: first of all variable names.

So, we talked a little bit about variables. Variable names have to start to a letter,

start with a letter. Variable name should be incredibly obvious to what it's referring to.

So, for example, I used to work for a company, and, no joke, they had, um, I was looking at some code.

They wanted me to play with, and fix, and there was something called like zm52xr37,

or something, and it was equal to 89.3 or whatever was going on.

I was like, "What is Zm52XR37?"

And they're like, "Oh, that's wind speed."

I was like "but, but, why?"

Their like, "Well, because it was taken on the something blah blah blah blah blah."

I'm like, "but why don't you just call it wind speed?"

and they're like, "well that was back whenever we could only use eight characters for variable names."

I'm like, "but couldn't you just call like windspd or wndspeed?"

and they're like, "No, we named it xm52XR37." (or Z)... Whatever!

Anyways, don't do that! That's really, really, awful.

if you write code like that that means no one else can ever edit your code,

and you're like, "But then I have job security!"

But then, it also means it like you have to write that code the rest of your life

and you can never do anything funner than writing code with really obnoxious variable names.

another thing that you don't want to have is names like "NotTall"

because then you might have something...

so if you set "NotTall" equal to 1... that means that I'm actually short.

So, rather than setting a variable called "NotTall" equal to one,

you should call something "short" equal to zero,

wait, "short" equal to one. Yes, or Tall equal to zero.

Those are all better than not tall is equal to one.

All right, so just some some things to think about.

So, they do have to start with a letter, they are case sensitive.

So, I can have something like, you know, 'Kisses' = 5 and "kisses" = 6,

and then I can say "morekisses"equals "Kisses" plus "kisses," and I get 11.

So, "Kisses" is its own thing and then "kisses" with a little k is six.

So, 5 + 6 is 11.

Okay, so um, remember the things are case sensitive,

because sometimes what will happen is you'll be typing and then you'll be you know 50 lines down in your code,

and you're like, "why isn't my code working?!"

and it's because you misspelled something, or you, you, didn't capitalize it.

So, the same thing applies to misspelling.

So, if for some reason I wanted "kisses," times 2,

and I was like oh well then that's "kisses" plus "kises," but I misspelled it the second time.

It's gonna yell at me and it says, "You misspelled it!"

and you're like "but, I but, but that's not right I defined kisses is up there."

and you're like, "No, you didn't."

So, if I say 2 times "kises" but I misspell "kisses".

It's gonna yell at me, because I misspelled it.

Not, honestly, most of the problems you're gonna have at the beginning are gonna be related to like not spelling things right,

or not being consistent with capitalization and stuff like that.

There are a couple of reserved key words and you will know them whenever you see them,

because when you type them they turn blue.

So, if I wanted to say "if" equals 5.

I'll say, "'if' equals 5., and like wait, why did that turn blue?

If it turns blue you can't use it. So, there's a couple of others "for" turns blue, "while."

So, "if," "for," see how it's messing with me.

"Next"? Is that blue? No, "next" doesn't it turn blue.

"end," Turns blue. "do"? No. "loop"? No. Trying to think of what other ones, "break"? "continue"? Those turn blue.

All right, so, you can't have a variable called like break.

You could have "breaktime," and that would be fine.

So, if you really, really, want to use something that's blue change it.

I think capital "While" is fine you just can't use a little "while."

That said, don't use capital "While," because you're just going to confuse everyone who looks at your code.

So, if it turns blue you can't use it.

Let's see, you also can overwrite functions.

So, for example, there's a sin function.

So, I can say sin of zero, and sin of zero is hopefully yellow.

Ow come here girl, oh, I misspelled "kises" still, all right.

So, sin of zero is zero.

Okay, so, sine of zero is zero, and then it's sine doesn't work in radians, but that's not the point right now.

If I read, if I wanted to, I could define a matrix called sin. Equal to I don't know anything.

And that really messes up the situation because, now if I try to say well what is "blah" of...

Let me start a new section so I don't mess this up.

I did mention this in one of the other videos if you missed it.

I'm going to start I going to try to start a lot of my sections with CLC clear.

Umm, what CLC does, is it, um, it clears out this section here, the command window,

and then clear actually clears out my variables.

All right, so, let's say, let's say, instead of finding the sin of zero, I was finding the sin of one.

Alright, so, here we go, alright, so, what you'll see is it's gonna get kind of crazy.

So, when I first say what is the sin of one? It's 0.8415, which you're like, "well what does that mean?"

So, that's literally if I take the sin of 1, which is π over 180.

Wait, why am I doing it this way? Sin of 1...

Hold on... I got it... yeah... Oh because it's in... let me not have to think so hard.

I don't want to think hard right now. So I'm gonna put my my stuff into radians.

So, literally, it's just the sine of one. All right in radians, and you can't really see there we go.

So, that's the sine of the value one, and then but I renamed sine.

I called it, "induced matrix" called it "1,2,3" and now the sine of one is one.

So, if I called sine, you know, "45, 99, and 103." Whenever I first run it, it says, "Oh sin of one is 0.8415, yay!"

but then I redefined sine and it is equaling this really weird matrix,

and it's like, "Oh you must mean that you want the sin meaning the sin and the first element of the sin matrix,

in which case it's 45."

And then, your manager freaks out,

because he's like," how did you get the sin to something is 45 because sign goes from negative one to one."

You're like, "Oh because I was an idiot, and I over wrote the function name."

MATLAB will let you do this MATLAB will let you be an idiot, so don't do that.

In general, don't, don't name something you know, don't name something square, don't make something like this,

it says square root equals 55 and 22 because then if I want to find the square root of two it says it's 22.

So, so, don't do that. Don't do that. Don't, I mean MATLAB will let you.

It will NOT give you an error sign,

and I think part of that reason is there could be all kinds of weird functions that you don't know the name of,

and then you accidentally try to use them,

and reserving a keyword it's like really hard like computationally or whatever, so...

not computationally, but I know what I mean um, it's complicated so they let you overwrite stuff.

In fact, I can create my own sin function. I can actually rewrite the square root function entirely.

We're not gonna do that today by any means,

but basically if you're gonna write a variable you can't use keywords.

Um, come here. Here's your rules.

You can't use keywords. You have to start with a letter, you are case-sensitive,

and you can rewrite over existing function names, but it is dangerous.

So, I think in this example, I actually show how you can rewrite π.

So, if I say π with a capital Pi it's like, "That's not defined. Did you mean pi?"

You're like, "Thanks MATLAB! I did." It's like, "Okay."

but if I wanted... I can say more... So, I'm gonna do CLC clear again.

So, if I say, "pi",

Okay, so there's my PI, that's gonna be 3.14159.

I can just rewrite the state of π as 3, like that one state tried to do in like the 1800s.

If you don't know I'm talking about go google it. That's kind of funny,

and all of a sudden I've rewritten the value of π for everyone to see.

So, it was originally 3.14, like it was supposed to be, but now it's 3.

No fun, don't do that, that's just silly, and that's why a lot of times like I said,

I like to start things out with the CLC clear because then if anyone else did something goofy, I um, I get rid of it.

I'm doo de dooo do doo come here bro. Boop boop doop dee dee dee all right,

Clear, and just more stuff.

All right, so, there we go. So thats a good start.

So, like I was saying, if I just come down here...

So, remember anything that I type up here is just the same as typing it down there.

It's just that if I want to use it multiple times, I don't have to retype it.

So, if I want to type CLC, that's gonna clear this command window here.

So, CLC the command windows gone.

Um, say I want to type CLC again. I can hit C and hit the up arrow, and see how that CLC is highlighted?

That's kind of fun.

So if I just hit the up arrow in general I can go through with anything that I've typed and it works.

So that's fun,

um and then see over here in my workspace these are the variables. Remember I rewrote π (pi) as 3?

If I go clear. That's gonna clear out those variables.

If for some reason I had like x equals 5 and y equals 2.

If I only wanted to get rid of Y. I could just stay clear Y, and X would stay.

But anyway, I just like to clear everything out CLC clear. Got it? Good? Great!