Video: Example: using built in functions (7.32)

 

 

Now to find out how many items could be bought we can use floor function so I will type floor, I am dividing 50 by 3.35. So answer is 14. So 14 items can be bought out of $50. As you can see in the Workspace as well - answer is 14. Now to find out how much change we are left with we need to know how much is the remainder if you have to divide 50 by 3.35. So, I will use rem function. Make sure with the correct syntax. So I am dividing 50 by 3.35. So 50 comma 3.35, close the bracket and enter. So answer is 33.10. So, $3.10 is left as change. Now, have you noticed in the Workspace, your previous answer is overwritten by new answer? And my previous answer is gone? What  to do if I want both the answers saved in the Workspace with different names? A name which make sense to me and to other people. Yes we can name it. Let me recall my previous command, rem, what ever it was, this, so this was actually change, so I can name it, I can write change, equal to, rem 50, 3.35. So now it is saved in the new variable which is called change. Now as you can see in the Workspace, there is another variable change, besides the previous answer as well. OK. I can name my other variable too. So that was floor function, I recalled it. So I want to call it items. So if I say items equal to  floor 50 divided by 3.35 so it gives me items equal to 14, and now in workspace you can see another variable is saved which is called items. What if I wanted to name it differently? Instead of items, I wanted to name it 'number of items'?
This wouldn't be allowed. This is not a valid variable name. You will see error. So spaces are not allowed in variable names, but underscores are allowed. So you can define your variable using underscores - that is valid. There are certain rules for naming a variable. Suppose you want to clear a particular variable from Workspace, say you want to clear items. In Command Window you just type clear items. Enter. So there is no items variable in Workspace now. Suppose you want to clear another variable say ans, just say clear ans. It is gone. If you want to clear all the variables from Workspace, Just type clear or clear all, they will both do the same thing. So now there is nothing left in Workspace, all the variables are cleared. If you want to clear the screen, the Command Window, you can type clc. See it is just clean now.

Okay, let's do a different exercise. Suppose you have $50, and you went to buy certain items - each item costs 3 dollars and 35 cents. Okay, let me write so you can see. So you have 50 dollars and each item cost 3 dollars 35 cents, 3 point 3 5. And the question is How many items can you buy? And how much change you have left? How much

change?

 

Okay, note that you can not type any sentence like this in MATLAB - it is invalid. So suppose you type something like this and press enter you are going to get an error message - see, like this.

So, what should we do if we want MATLAB to ignore any comment or any sentence like this? We can put a percentage sign in the beginning of the sentence. So if I put a percentage sign in the beginning, it becomes green, as you see, and this whole line, which is followed by this percentage sign will be ignored by MATLAB. It wouldn't be executed. So if you press enter, it is not going to give you any error message. Okay?

Now to find out how many items could be bought we can use floor function so I will type floor, I am dividing 50 by 3.35. So answer is 14. So 14 items can be bought out of $50. As you can see in the Workspace as well - answer is 14. Now to find out how much change we are left with we need to know how much is the remainder if you have to divide 50 by 3.35. So, I will use rem function. Make sure with the correct syntax. So I am dividing 50 by 3.35. So 50 comma 3.35, close the bracket and enter. So answer is 33.10. So, $3.10 is left as change. Now, have you noticed in the Workspace, your previous answer is overwritten by new answer? And my previous answer is gone? What  to do if I want both the answers saved in the Workspace with different names? A name which make sense to me and to other people. Yes we can name it. Let me recall my previous command, rem, what ever it was, this, so this was actually change, so I can name it, I can write change, equal to, rem 50, 3.35. So now it is saved in the new variable which is called change. Now as you can see in the Workspace, there is another variable change, besides the previous answer as well. OK. I can name my other variable too. So that was floor function, I recalled it. So I want to call it items. So if I say items equal to  floor 50 divided by 3.35 so it gives me items equal to 14, and now in workspace you can see another variable is saved which is called items. What if I wanted to name it differently? Instead of items, I wanted to name it 'number of items'?

This wouldn't be allowed. This is not a valid variable name. You will see error. So spaces are not allowed in variable names, but underscores are allowed. So you can define your variable using underscores - that is valid. There are certain rules for naming a variable.

If you look at Section 2 of the website you will see that there are rules given for variable naming. So variable names can contain up to 63 characters. Variable names must start by a letter, followed by a mix of letters, digits or underscores. No other symbols and no spaces are allowed. So there are some examples of valid variable names. Also variable names are case sensitive. So crows, crows with C capital, crows with ROW capitals, and crows with all capitals - they are all different variables.

Suppose you want to clear a particular variable from Workspace, say you want to clear items. In Command Window you just type clear items. Enter. So there is no items variable in Workspace now. Suppose you want to clear another variable say ans, just say clear ans. It is gone. If you want to clear all the variables from Workspace, Just type clear or clear all, they will both do the same thing. So now there is nothing left in Workspace, all the variables are cleared. If you want to clear the screen, the Command Window, you can type clc. See it is just clean now.

 

Last modified: Monday, 10 August 2015, 10:20 AM