In today’s lesson, we will be covering how to create input statements.
Input statements are used in scripts to get the user to input a specific number rather than assigning one value to a variable. For example, if I wanted to make a code that added together two numbers that someone using my program would pick, then I can’t define those numbers in the code because I don’t know what the person will pick! In that case, we can make an input statement so the user can enter numbers or letters, then the rest of the code runs using those values.
Let’s take a look at how this works. We can make input statements for the user to enter numbers or letters, depending on what your code is doing. We use the syntax variable=input(‘Enter prompt here’), where the words enter prompt here are replaced by the question you will ask the user, and variable is replaced with the variable that will be equal to whatever the user types in.
Let’s do an example where I want the user to enter two numbers so my code will add them together. In the image below, each input statement will only accept one number, because each number is then assigned to a variable as I mentioned above. So I have two input statements because I need the user to enter two numbers. Firs I have x=input(‘Enter the first number’) then y=input(‘Enter the second number’). This means that the variable x will equal the first number that the user enters, and the variable y will equal the second number that the user enters. Note that the words you’re typing after the input (like I put Enter the first number), need to have single quotes around them, and the words turn purple when you do this.
In the third line, I have x+y, which will add the two numbers together. I also added the comment (in green) that explains what the third line line does (click here for a refresher on adding comments).
Now when I run the code, this is what comes up in the command window. It asks me to Enter the first number. This shows that whatever you put within the single quotes (the words that become purple) will be exactly what is shown in the command window to the user.
I type in 5 as the first number, hit enter, and the second prompt comes up, asking me to enter the second number. Again, this is exactly what I had typed in the code that is showing up as a prompt.
I type in 4 as the second number, hit enter, and the next line that pops up is ans = 9 in the command window. This is because after the two input statements, the next line for MATLAB to execute was x+y, which added the two numbers I entered together. This gave the answer as 9.
Now what if we have a code where we want the user to enter text instead of numbers? This is otherwise known as a string – it can be letters, numbers, special characters, or anything else. MATLAB will treat this as one block of text, so even if you entered numbers like 845488 or 845%39^2 or something else, MATLAB won’t use them for calculations. I’ve usually seen this used when we want to enter letters though. We can do a lot with this, but we haven’t gotten to switchcase or loops yet. So I won’t get into what we could use this for, but I’ll just show you how to create them.
In this example, I have created an input statement where MATLAB will treat whatever the user enters as a string, and I’ll be typing in letters, rather than numbers, for the answer.
The syntax you should use is variable=input(‘Enter prompt’,’s’) where the words Enter prompt are replaced with whatever you want to ask the user, the word variable is replaced with the variable you want to store the user’s answer in, and the letter s should always be there to indicate to MATLAB that it is a string. There need to be single quotes around the prompt as well as around the letter s.
You can see that I did this in the example below, so everything typed within the brackets should be purple. When I run the code, you can see in the command window that whatever I had typed in the input code pops up, and asks me what my name is.
When I type something and hit enter, MATLAB will store my answer in the letter x, because that’s what I had set my input statement equal to in the beginning. This value can be used later on in the code if I have other operations to perform.