M-files, disp, and fprintf

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is to to me MATLAB with my second video m-files f printf and disp you open a new M file by clicking this icon right here some of the first things you want to do before you start actually writing your code is to comment at the top and within your code so I'm going to start with the top of the of the file to tell the computer that you're going to start writing a a comment you use percentage the percentage sign and usually I like to start my files with my name and then also the percentage sign will only work only work for things written in that line so if you want to write another thing in another line you have to do percentage sign again so almost a month it may be the name of the file I'm going to call it example one dot M that's the extension and maybe what the file does teaches people how to use MATLAB and now you can actually start writing your code so let's say this code will have a variable for apples have a variable for oranges and we'll compute how many pieces of fruit you have so you can do and you will you yourself will decide in the future when where you want to comment and where it's necessary I'm just going to over a comment in this M file just for the sake of showing you guys how so this variable tells you how many apples and now I actually write code apples equals 10 this variable tells you how many oranges this variable tells you many apples plus produce fruit equals and I'm not going to suppress this so that when I type the name of the EM file in the command window this whole code runs and I get this output so I'm going to leave this like this and I'll save it as example one no spaces now I'm going to go and type example one you notice how I didn't put dot M here that's the correct way to do it and to be able to run the code it has to be in your current directory so just make sure you notice where you saved the file and that this is the same directory where you saved it and you can check in your current directory there's my file so fruit fruit equals 30 and I have apples fruit and oranges right here and all the values um so a neat thing you can do with this is actually display messages and values of variables so let's keep all this let's suppress this so that it no longer tells us what fruit is and then I'm going to display with a nice message hey this is how many pieces of fruit you have and then display the number so to do this you can use either display or f printf so when you use display you can say the format for display and I'm going to put it up here I'm going to do there's two ways to print first is disp and the second one is F print F and between this parenthesis is what you want to print or display so to display a statement you have to do use apostrophe and then write your statement and then close it with another apostrophe and then close the parentheses in the display so I have display and I can write this is how many pieces of fruit you have close close and you don't suppress because it's displaying something and then and then I'll do disp and then I'll write the variable fruit and if you see I didn't put any apostrophes here because I want to display the value of the variable fruit not the word fruit if you want to see the difference between those two I can do this fruit I'll just use apostrophes here so this will print this is how many pieces of fruit you have it'll go to the next line because disp automatically goes to the next line and then it'll print whatever value this is in this case 30 and then this will print the word fruit so any piece of fruit you have and then fruit so yeah that's one way to print and then the other way to print is to use F print F I like to personally use F print F all the time because it's more versatile the thing is that with display you can only either display strings or you can only display one variable and with F print F you can display values of variables within your string you'll see what I mean when I do this so I'm just going to keep this I'm going to erase this one and keep the rest of the displays so you can see how F print F works exactly the same way as disp so I can do F print F open parenthesis and then I can write whatever I want this is how many pieces of fruit you have but what Efrain f does not do is go to the next line automatically so if I were to put another F printf below this one it would print on the same line I can maybe I'll show you that'll be best F print F this is still on the same line or an example one and what i'm doing here is pressing the the UP button in the cursor and I can cycle through everything I've I've written before so example one the so this is the disp and then this is the friend this how many piece of fur do you have this is still on the same line and this is the current lineament these two greater than signs mean that I'm on that line and you don't want that you don't always want this to be the last thing on your code and on the like on the line on its own so I'm going to go here to tell F printf that you have to go to a new line you do backslash N and then I'll do backslash in here and now I'll have what I want so I'll do example one this how many piece of fruit you have this is the same this is how many pieces we have and now on a new line is still on the same line and this is nice and tidy in the last line by itself now if you want to display variables within this statement let's say I want to write you have and then the number of things a number of pieces of fruit you can use placeholders and so let me get rid of all the displays and I'll put a comment here placeholders there percentage sign D for numbers percentage sign s for Strings there's a bunch of other ones but you can look them up on MATLAB comm just use just Google placeholders for F printf and they'll give you a whole list for the different placeholders you need to store different things so I'll do F for an F and maybe I'll change the message a bit I can say you can sell and my ultimate message I want to look like this you can sell X number of apples and Y number of oranges so that's my ultimate message so as you can see I have two variables that I don't know the value the actual value depends on what these values are ultimately so I have to use a placeholder and in this case I can choose between these two and D is my integer one you can also use F if you have decimal number with decimal points and you can use F for that percentage F that is so you can do you can sell percentage D number of apples and again percentage D because it's another integer number of oranges right and I'll do period and go to the next line and I'll close this message so this is what it'll display now to tell the computer that you want this percentage D to be the value of apples of the variable apples and this percentage D to be the value of the variable oranges at the end you're right comma and however many variables you used and in the order you use them so I use the variable apples comma and I use a variable oranges and those are the only two variables and I use them in that order and that way MATLAB will know what to put where close the parentheses and I'm pretty happy with this statement I'm pretty sure it'll work so let's save it just go to MATLAB let's clear the whole command window clear command window enter well nice and tidy and I'll do example one again you can sell 10 number of apples and 20 number of oranges and there you have it that's a perfectly legitimate F printf statement and you can do this also with with strings I'm not sure if I mentioned that in string variables in the last video but I can say x equals hello there and I'll close now this is a variable that's a string it's a it's a message by itself so maybe I can do at the after this I can do F print F and then um I'll do ' percentage s and then I'll do next line close apostrophe comma what variable did I use I use X close the parenthesis and okay so didn't use any other variable so I only print an X and I'll do example one you can sell set number of oranges and hello there so if you change the percent s here to an integer value it'll actually print the ASCII values if you're not familiar with ASCII values that's fine and will certainly hit that topic in another video so percentage D I'll save and I'll do example one and these are the SQ values of the characters i had which is certainly not what you wanted you wanted to display the message hello there I think that's it for displaying an F for an F and next video I'll show you how to use user inputted data and you can actually print very nice statements with user and put it data I'll see you next video hopefully I'll get it out sooner rather than later please subscribe that's what keeps me going and making more videos I hope this was helpful and I sure appreciate constructive criticism thanks a lot
Info
Channel: TutorMeMatlab
Views: 71,050
Rating: 4.8426967 out of 5
Keywords: fprintf, disp, help, matlab, beginner
Id: 4lVesnrYneA
Channel Id: undefined
Length: 14min 54sec (894 seconds)
Published: Sun May 29 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.