Writing Input Statements in Matlab

matlab user input
This is a topic that many people are looking for. thevoltreport.com is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, thevoltreport.com would like to introduce to you Writing Input Statements in Matlab. Following along are instructions in the video below:

Were gonna start off here, and were going to look at different ways to do user defined input, or user input, output, and all that kind of stuff. This is really, really important, because, you know, its kind of hard to write a program with all the inputs directly in there, you know, like whenever you buy TurboTax. You dont just, like, start it, and it goes, “You owe a thousand dollars.” Youve actually got to put an input, and then it gives you output based on that input. So, this is a really simple thing to do, but, well…yeah. Its a basically simple thing to do. Its just something that you want to get started with. So, were gonna start with a couple of different ways to do…well, theres really only one way, kind of, that we wanted to user defined input. But, lets call this, um…”Input.” So, its really not that hard. You just ask the user for what you want, by saying, “input.” You say what… I dont know, whats a good question to ask? How many hot pockets did you have for breakfast? Alright, so look at the syntax lists. Ive got the word input, an open parenthese, and then the prompt is going to appear in these little apostrophe thingies. So, if I go, “How many hot pockets did you have for breakfast?” And I run it, its gonna ask, and Ill say, “I had 7 hot pockets.” Alright. Good? Make sense? Now theres some things that we can do to make this pretty, because this is what the users actually gonna see, so as ugly, and stupid, and weird, and quirky as your code is, you want your input statements, your output statements, things that the user sees, to be very clear. So, Im going to start and make that capital, and then its gonna.. let me put a “clc” clear at the beginning of my statement, just so it clears out every time. Alright. How many hot pockets did you have for breakfast? 7. Yay! See, now it looks a little bit better. This is kind of funny. See how theres a question mark, and then it goes straight to 7? So, what I can do is I can come over here, and I can actually put in a couple of extra spaces, and then itll print the spaces, before I type the 7, which is just kind of neat. Something that you may have noticed, so this will happen sometimes. The students will say theyll run the section, and then theyll be like, “Oh man, I didnt really want to know hot pockets! I wanted to know how many…” I dont know, what else do people have for breakfast? “SpaghettiOs…. Did you have for breakfast?” And then they will run it, and theyre like, “My codes not working, because I changed it to Spaghettios, but it still has hot pockets, but there is

kind of a trick. This is how you know that somethings going on. Basically, what happens is I asked… what happened was, is I ask the user for input, but I never actually gave it input, and then I try to change it again. Did I spell Spaghettios right? I dont think it really matters, but anyway. If you put your little mouse up in the text editor, and it spins like that, that means that its waiting for input from you. That means that we havent actually finished the last time that we ran the program. This can happen whenever youve got an input statement; it can also happen when youve got, like, an infinite while loop and stuff like that, which we havent talked about yet, but anyway. So, the way to get out of that is youve got to come back and you have to click back down in your command window. If your command window hasnt turned blue, you havent clicked in it correctly, and youre gonna hit “Ctrl C.” Thats gonna break it. Now, you see, its asked me for SpaghettiOs, because I actually clicked the run section a couple of times, so Im just gonna keep clicking “Ctrl C” until it all clears out. Now I can come up here again, and I can run my section. How many SpaghettiOs did you have? [Inaudible mumbling] Yay! Alright. Something else to keep in mind is whenever youre asking your user for stuff, they dont really need to know where youve stored that variable. So, Im gonna suppress the output. Do you remember how to suppress the output? Maybe? Maybe? Kind of? You suppress the output with a semicolon. So, if I put in a semicolon, thatll suppress the output, so now, when I say, “How many SpaghettiOs you did you have for breakfast?” And I say 9, its like, cool. Alright, so the information is still there. See over there? Z is still equal to 9. it just didnt display it to the user, because the user doesnt really care what you call the stupid variable. It just wants to know, I dont know, whether or not theyre having a good breakfast, I guess. Alright? Make sense? Now, Im gonna go ahead and save this answer. My notes…my notes…alright, so got my notes. Now, try to run your section again, and you might have already done this inadvertently, and did not know why it didnt work, but say I had 3 cans. I type in 3 cans… I get an error. Alright, this is important. Matlab does not like it whenever you mix text and numbers. Well, it can, but then it all has to be text. So, were gonna have to have a little bit of a discussion about this, but basically, the idea is whenever I have a question, like “How many SpaghettiOs did you have for breakfast?” Something thats put into those little

apostrophe thingies, thats a string, okay? The word Im saying is a string. So, string is like a string of letters. So, its like a string of characters. Thats different than, like, a number. So, like, if I just wanted to say… I dont really have a have a good example…Im gonna say 5 chickens. If I just type that in, itd totally freeze up. I get the same kind of error. So, whenever I come up here, if Im going to be expecting user… theres…theres actually two different ways to do it, so heres one way: is I can say, “How many SpaghettiOs did you have for breakfast?” I as the user could know how Matlab works, and I could respond with 3 cans, but put it in two little apostrophes. Okay? So, if I put ” 3 cans ENTER,” its totally fine. Now over here in my work space, you see that it says 3 cans. Something that you may not have noticed though. Let me just make another line that does this exact same thing, except Im going to call it y. “How many SpaghettiOs did you have for breakfast?” So, first of all, Im gonna say 3, and then Im gonna say 3 cans. Look over in your work space; you actually see that youve got two different symbols next to y and z. So, y says 3, z says 3 cans, and it has the little apostrophe thingies on it. If I scroll out enough, its gonna call it a class as a double, and a double actually indicates how the computer stores the information, but it basically means a number. Its a 1 by 1 number, right? Remember, we talked about how Matlab stores everything in matrices? So, even though its a 3, it is a 1 by 1 matrix. This is a character string, and see how its got little “abc” on it? Thats an indicator that its a character. Its actually a 1 by 6, but its just kind of funny, cause thats one, two, three, four, five, six, so 3 with a space and the word “cans” has four characters, so its actually stored as a 1 by 6. Thats kind of above and beyond what you need to know, but whenever Im programming something, I probably dont want somebody to say, “3 cans” because, say I was going to multiply, I dont know, how many calories are in a jar of SpaghettiOs. Its probably like 320 or something. So I would say y times 320. Im gonna go ahead and take this guy back out. Im gonna leave it as z because thats what I had to begin with. So, Ill be like, okay, so my calories is z times 320. That works, but if the user puts in 3 cans… this is a really weird thing. Dont freak out. Basically, whats happened is Matlab has gone through and figured out

the ASCII code for all of these different letters, and then multiplied it by 320. If you just pretend for a minute that a can of SpaghettiOs is one calorie, and I put in three cans again…so, this is way beyond what we need to know right now, but basically, like, the character 3 corresponds to the number 51 in ASCII code, and space is a 32 and the c is a 99. If you super-duper want to know what were talking about, you can come to here and you can google “ASCII table.” So, whenever I say “ASCII,” thats how I spell it: A-S-C-I-I. ASCII table. ASCII table. I like this one, and if I come down to 32, see how 32 is a space, and 3 is a 51? So, my number 3…my character 3 is a 51 in my ASCII chart, so thats why whenever I put in a 3, I get a 51, and my space is a 32, so you would expect C-A-N-S to be 99, 97, 110, and 115, and in fact, that is what you get. Notice, this has to be a lowercase c, because lowercase c is 99. Capital C is 67. So, anyway, you can get all kinds of crazy stuff if youre not careful. So as a programmer, so that we dont end up with number of calories being some completely nonsensical value, what I could do is I could say, “enter number of cans” like that, and then that way, my user would know, okay, dont actually type in 3 cans. That kind of make sense? Alright. So, weve done…and in case you want to have this word down here, the word I was doing was ASCII… where the…and letters correspond…anyway. Alright, so that said, you might say, well, sometimes I want to know how many…I want to know, like, stringing information. So, lets go to string input. So, Im gonna tell Matlab to ask for some specific information. So, Im gonna start with a clc clear, and Im gonna say, I dont know, Im gonna say name, so, like, I can say, “input whats your name?” Now, a bunch of things have gone wrong very quickly. You see how I have “whats your name?” So, whats basically happened here is that Matlab has seen an open character, or a little apostrophe thing, letters, and then a closed apostrophe thing, and its like oh, that must be the text you want, in which case, why are you adding all this junk? So, the way this is reading right now really tells me that what Matlab thinks Im trying to do is have an input that just says “what?” What? But it doesnt. What Im really saying is “whats your name,” so, in order to actually display, now I could just say that: “Whats your name?” No worries, and that would work totally fine. That said, another way to actually make

it look pretty would be to put a double…double thingy there. Double apostrophe. This is not a quote, so this was not a one thing. In fact, if I do that, its actually gonna look goofy, so… My name is three, but if I come in here and I go double apostrophe, Ill get “whats your name?” My name is three. My name isnt really three, my name is bugaboo. My name is bugaboo, and then it gets sad because, you know, thats not a number, and Matlab only likes numbers, so its gonna keep asking me. So, unless you really want to fix this…well, you cant really fix the code. I could give it a number, but remember what I do if I come up here and I see that little thingy spinning? That means I need to come back down on my command window and hit Ctrl C. Okay? So, again, “hit Ctrl C to get out of a bad code run.” If you get really desperate, you can just exit out of Matlab altogether, but then youve lost everything since you last saved. Speaking of which, go ahead and save, because its fun and stuff. Alright. So, whats your name? Now, Im gonna run this now. If I wanted, I could spell bugaboo and put it into codes, but whats the chances that your user actually knows that? So, theres an extra character that we can do to the input statement. We put a comma and then s. S stands for string, so “S stands for string.” So now, when I write “whats your name” I can just type “bugaboo” and it works. So, what that does is thats a little alert to Matlab that says, “Hey, my user is gonna put in a string, so expect a string and dont freak out. Dont treat this like a number.” Kind of makes sense? So, the thing is to be careful about, if say I had the number of cans and I said, “input how much SpaghettiOs…how much,” Im gonna say much food so I dont have to spell all that again. “How much food?” And then I did this, so my name is still bugaboo, and my food is 3, but look it…over here, cans has been stored as 3, so you cant just always use a little s thing and hope itll work. You really have to do it when you need a string and not do it when you dont need a string. So Im gonna take that out, so I dont confuse myself later. Okay, but the s stands for string. Kinda make sense? Alrighty. [Singing] Doo-doo-doo-doo-doo-doo. Beautiful. Alright. So, thats really the most of what you need to know for string input. The biggest things that are gonna get you on here is forgetting, like, the double parenthesis, double apostrophes, not remembering to put the string command on there, and that kind of thing.

MATLAB (Programming Language), input, string, programming
Thank you for watching all the articles on the topic Writing Input Statements in Matlab. All shares of thevoltreport.com are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.

Leave a Comment