Using fprintf Statements for Numbers in Matlab

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Now what we're going to look at is, how to use fprintf functions and stuff like that. So if you've ever programmed in C, fprintf is something that you use all the time. If you're in C++ you're probably using CL. But up fprintf is a great way to be very, very, very controlling. So if you're a super, super controlling person, then fprintf is like the most amazing thing because, you could pretty much control absolutely everything about it. So there is a bunch of fprintf formatting things to learn. There's fixed point, exponential, decimals, general character string, but also slash N thing. So it comes down to this, say I-, I don't want to say: Fprintf, you are a snuggly wumpkins. And so if I do that and I run it, it's like you're a snuggly wumpkins. Then look what it actually does. It actually ends my prompt right here. So I would say, X = 5. So if I wanted to fprintf, you are smuggly wumpkin, and then I wanted to do it again. And I'd say like, no for reazlies... No, that's not how you spell for realzies. No for realzies, you are. So I say you're snuggly wumpkins. No, for realzies you are. I go like this, and I play it. Then look what happens, this goes here, and then this goes here, and then the little prompt thingy happens right there. So there's actually this little thing that we do. I think it goes on in the inside? Yes, it goes in the inside. This little thing right here, backslash n is a newline character. So, backlash n..means new line. I can actually put in like two enters. Like I could forcibly double-space it. I could put like just four there for no reason. There I got like a little slash N thingies... No! Snuggly wumpkins! Like that! And it makes me happy. Okay so there's that. That's kind of like the basic fprintf thing that you just absolutely have to know in order to move on. That means new line. Then we start off.. which one do we want to do first? Let's do that fix point. So if I say umm.. (CLC clear) Okay, I'll say x equals like five or something. So I'll be like okay X = 3. Then I want to say wait, that's not 3. ( laughing) I don't remember whatever I said. We'll make it totally different, X= 13. So if I line fprintf, I could say I ate... Well how I used to do it, is that I would do like num to string. I'd say, I ate 'num to string' hot pockets. But if I want to say, I ate 13 hot pockets, I could say 'I ate %f hot pockets.' Then what I do is, I put in a comma and I'm like X. That's what I want you to put there. So basically this percent F thingy is kind of like a placeholder, where your numbers going to go and then the number you're gonna put there is X. So I ate hot pockets. Okay but I didn't put the new line, so it looks stupid. So I'm gonna go ahead and put the new line in there. I ate 13.0000 hot pockets. I'm very, very, very certain about the precision of the number of hot pockets that I ate, because I can count. Okay so, that kind of looks stupid. So let's do basically the same thing. Let's see what else can we use? We can use... e? We can use e. I ate 1.3 times 10 to the first Hot Pockets. I guess? Um.. there is G, I think? G and D and C. Alright I can say I ate D. D is a decimal, and then G is..I think G is like general? (Oops space hot pockets) What I think the G does, is .. and I'm gonna look it up..yeah. It kind of does the best guess. I'm gonna call this, Matlab's best guess. So percent G is Matlab's best guess. F is fixed point. So, like a certain number of decimal places. E is obviously exponential. D is decimal. I don't know why they call it decimal because it doesn't show decimal. Here is a better explanation.. Oh yeah! It doesn't include trailing zeros. Umm.. I gonna say "ish" ..decimal ish. Most the time you're going to use F or G. So anyway. So if I want to eat that many hot pockets, I can. So yay! We can eat hot pockets all day long. Okay. So, to give you a little bit more idea how this works, say I wanted to say X = 12 and Y= 18. I could say if I ate like hot pockets and what else do you have for breakfast? Fprintf, I ate.. I'm going to use G, because I don't want it to show the decimals. It's gonna usually be a good job of catching what I want. I ate that many hot pockets and that many... I almost said rotary saw but that's not right. You don't eat rotary saws. What else do you eat besides hot pockets. Um, um.. Why is this the hardest thing that I have to do all day? Not a couch potato. That's also not something you eat. What do you eat? Um..I had a sandwich. Okay this is really hard. I have to remember to do the new line. Close it off and then hit a comma. Now whenever I put in x and y, it's going to put that then in the order that I listed it. So if I put X and Y, then I'll do X and then Y. If I said Y and Y, It would list Y twice. 18 and 18. So we don't have to be in alphabetical order by any means. It's just whatever order you put them in, is the order that they're gonna show up in. That's actually pretty cool. You're like, okay well maybe I can see the appeal of this, but this really isn't that fantastic. Well, wait till you see what we have in store! Okay. So let's say that I didn't have anything nice and pretty like decimal. Like pure integers. So like say I had 6.5 and then this one was one-third. So this is going to cause all kinds of problems. So if I say percent g hot pockets again, and percent g sandwiches again. (X and Y) So you can see how that's gonna look. So 6.5 hot pockets. That many sandwiches, that's very very precise sandwich I ate, 1/3 of a sandwich. Okay so that's kind of goofy looking. So I can fprintf, I ate.. No. I can actually control the precision by saying point 2f. That's gonna give me two decimal places with a fixed point thingy. Hot pockets and %2f sandwiches. Like that. So see, now I've got a two-point decimal precision here, and a two point decimal precision here. So that's kind of neat. Um.. What else can I do? I'm going to erase this guy. I can actually control.. how do I say this? How much space this takes. So say for example, I had that many hot pockets and that many sandwiches. Then I had more hot pockets and sandwiches. I'm doing like a thumb drive.. not a thumb drive, an eating drive of food. Talking is really hard today. I ate... Ok so, I'm gonna do this again. I can't stop saying whiches. That's funny..(laughing) So there's X and Y, and they're gonna go A and B. Okay so now I fprintf this, now I'm like well this looks kind of stupid. See how this is like kind of scoot over, and I kind of wish that like they took up the same amount of space. So one of the things I can do is, I can say okay, well this guy took up 1, 2, 3, 4, 5, 6 spaces. (counting to 6) 6 spaces. So what I could actually say is, I can force the other guy to take up six spaces as well. See, and now he takes up 6 spaces. This guy takes up 1, 2, 3, 4, 5 spaces. So I can force the guy above him to take up five spaces as well, and then everything lined up, and it kind of looks pretty. So this is why they're going to say if you're total control freak, Fpintf statements are amazing because they do allow you to have that crazy awesome level of control, or what else you might want to call it. Or you can really force things to line up in this way. Now one of the things that's a little weird about the fprintf statements, they don't exactly work precisely the way you think they're going to. The way I normally do stuff. So Um.. Let's make a table of stuff. I am going to go 1 - 10. Then I'm gonna make a table, it shows like x². Okay and then I'm gonna make a table that's X and blah. So I've got this little table. ( I might need to do 1-5, that's too many to go down) So there we go. So there's my little table. Now if I want to use fprintf to print this table, This is.. Okay so just check this out, because this is kind of crazy. Kind of super awesome crazy. Okay so I go fprintf, come on girl! Fprintf. Blah. I'll just do this, or I could say that squared is that. You're like, okay. Well I'm kinda making progress. I'll make it g. Because g's are general. They're not really general but that's okay. So then I'll say, I want you to do table. I think that's how you do it. So you're like wait a second? This isn't right. (laughing) This is not right at all. It really makes absolutely no sense. Okay this is too much fun. Okay so we have 1 squared is 2, 3 squared is 4, 5 squared is 1, 4 squared is 9, and 16 squared is 25. You're like no, that makes me so sad. Okay so, then you really have to look at what's going on. So I'm going to display that table, really nice and pretty, the way we used to do it. Like that. So that you can kind of see what's going on. So this table, the way it's supposed to look. 1 squared is 1, 2 squared is 4, or 3 squared is 9. Okay? But right now it says this 1 squared is 2, and then 3 squared is 4. So hopefully you're seeing that in MATLAB because it thinks in columns, It's going down the stupid column first. So it's going 1.. 2.. 3.. 4.. 5. Then this guy right here. So it's saying 1 squared is 2, 3 squared is 4, 5 squared is 1, 4 squared is 9, and 16 squared is 25. Okay. So that means that if you really, really want to do something like this, that you have to actually invert the table to where your your rows that you're trying to display are actually your columns. See then that's gonna fix the problem, because if I display..the what you call table.. The transposed table. So now I have 1 squared is 1, 2 squared is 4, 3 squared is 9, 4 squared is 16, and 5 squared is 25. So the way that you get around this weird table thing, is to actually transpose the table. So this is important because this is actually a really, really good way to do this kind of stuff. Is with a fprintf statements, because say you had like.. You know some degrees or you know, say it between 0 and 90. You had 10 of them or 5 of them. Then you want to do, sines are sine of degree. And then you want to do cosine, cosine (degree), then we are going to do tangent because why not? Then you make like a little table. It's degree.. can't spell. Sines, cosines, and tangents. Okay, now we have to disp (table). So I've got this. This is lovely and this is fine, but what you could go and do with the printf statement is, you can actually go in and control like how much fidelity do you really want to see in these numbers? So I could come through, and I could actually create like a really well spaced little thing. So I could say fprintf. I can't spell that for some reason. So I could just start off by saying, Okay, well there's %, fprintf, umm... I could even go... I think I could do that? Let me try that for a second and see what happens. So I can actually say % f, %f, %f, new line because there's four of them. Then they all kind of smushed together. I think I can do this, I'm gonna play. Du doop, yay!! And then du doop, and then du doop. You're like what is this craziness? Okay so this craziness is the tab. Tab, tab, and that's can neat. I don't know? I just think it's kind of cool because it looks like complete garbage. Right now it is, because I didn't do that. But it's still kind of garbagie. Which is kind of crazy that it tabs it over, and it looks weird. It's kind of fun. So, now I could say well, I don't want to show all that extra stuff. So I don't want to show all those decimal places. I want to show a total of say five characters with two decimal places. The decimal point takes up one of those characters. So you're going to show two decimal points a.. or two like fractional numbers like tens and hundreds. Then they show the tenths place, the hundredths place, a decimal and then two numbers in front of that. Potentially. If they are there. So that's what I need. So the hundreds place, tenths place, the decimal point, and the next two numbers if they happen to be there. So then I could do that over here too. 5.2, 5.2, then 5.2, come on. Okay, and you can even say, well for the first one I really only show one decimal place because all the other ones are zeros. Then it lines it actually up on the decimal, and that's something that you don't necessarily get here. The zero is way over here, and here the zero is nice and lined up with the other things that are going on in your column. So it's actually kind of neat. I can put tables, and I don't know if this is gonna work. I think I can say degree, sine, cosine, tangent. There we go, and then if I don't like the way the tabs look, I can always go at add an extra space here or there, and do that. In fact I don't even have to put the tabs in there if you don't want to. You can just physically space them out. It's not a problem either. It just takes- the tab kind of gets you close to the right place I guess. Something like that. So, fprintf really does give you a lot of control, you know. So since tangent gets pretty high pretty quickly, you know if I was going to like instead of going from 0 to 90, maybe I was going from 80 to 90. Well, those numbers don't get that big that fast. Say I was going from 89 to 90? See how these numbers are getting fast, and they're starting to get pushed over. So I can actually change it to where instead of showing it as a as a fixed point I could show it as exponential. So remember e is for exponential, and then it would show all those, you know, kind of like that if I wanted to. If I was really getting up that high, so I mean it's just something else to think about is they don't all have to be done the exact same way. You can play around with it and get different values, and even here on the sine because it's gotten so small, I can't really see- I can't see the difference. Whoa. Like that. So there's just all kinds of things you can do with fprintf. You have a lot of control over how you want to show things. You can show things with different decimal places, different widths, different sizes. The biggest thing that a lot of times will get people when they're displaying tables is not to remember that little apostrophe. That transpose signal at the end of the table, so that since MATLAB does read in columns, that you don't actually want it to display by columns, you want it to display by row. Now you might even wonder, well why do we even put things in a table to begin with if we're just gonna have to mess with it later? So why couldn't instead of just doing this, why couldn't I just say put it in degrees, and in sines, and then cosine, and then tangent, and I just have it read it through like that. The answer is actually kind of straightforward and simple, so MATLAB again, because it's thinking in columns, it's not actually going to do the first one from here, the first one from here, the first one from here, and then the first one from here. It's actually going to run through all of the degrees first then through all the sines, then through all the cosines, and then through all the tangents. I'm going to go back to my normal pretty words, but now you can see what it's doing with degrees. The degrees were a 0, 22, 45, 67, and 90, and then the sines were 0.38, 0.71, 0.92, and 1. The cosine was 1, 0.92, 0.7, 0.38. So I'm pulling these numbers from here, but you can see that these columns are now being done like this. So it's really, really, really important to put the information into the table. If you don't put them into the tables first then when you actually try to run them with MATLAB, they definitely won't work. Also a little quick note about the this backslash n's. It's to make sure you understand that you can use this to make really obnoxious code because if you think that this is obnoxious, I mean that looks pretty stupid. If you ask me. fprintf. So I can actually say, "I like to put things on a lot of different lines and there's nothing you can do to stop me." Now I know that's kind of hard to see. So I will put a little dot dot dot so you can- Oh really? You're not gonna like my dot dot dot. You don't like dot dot dot in fprintf you little snug! All right well there you go. Well I like to put things on a lot of different lines, there's nothing you can do to stop me. So know how this is going. So I like to put things, and it almost looks like I'm saying "\non," but I'm not. I'm saying, slash on instead of saying "\nand" I'm saying slash and, Instead of "\ndo" , it's \n do stop me. And I can put that there, and in fact if I put the new line here before the period, you would see the new line would go, then it's a period and then it's going again. So it does like exactly what you want it to- what you tell it to do, which is a little weird. Another special character is say I wanted to say, you know, if I had a percentage, X is 13 . For some reason X is 13 a lot today. 'I like this class' X %. Well I can't say X percent of the time. We'll say "G % of the time," and then I put an X, and I run it, and it says, "I like this class 13" and it doesn't do anything. So you're like, "but this is a percentage! How do I show a %?" Well kind of the same when you do the apostrophe sign. Instead of doing apostrophe apostrophe, you do "%%". Now I've got that stupid space in there, so I actually have to back it up. So I like this class 13%- I want to make this 90- Because that's better that way, 93% of the time. "\n" and everything's all good and happy with the world.
Info
Channel: SnugglyHappyMathTime
Views: 30,391
Rating: 4.8203592 out of 5
Keywords: MATLAB (Programming Language), fprintf
Id: SqmDC--uLCE
Channel Id: undefined
Length: 20min 32sec (1232 seconds)
Published: Mon Feb 09 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.