[MUSIC] We've been typing lots of stuff into the command window in response to MATLAB's little double greater than prompt here. I wanna clear all that away. Clear that work space and clear the window, there. Nice clean slate. What if you wanted to have your function get input in the same way that Matlab gets it in the command window? In other words, your function would print a prompt in the command window and allow the user to type input into it. Well you can make that happen by using the function named input. Let's illustrate it in a function that we'll write called One More. Let's see. There. It won't take any input arguments. But it will return one output argument, we'll call that A. And here's the code inside. The argument to input must be a string, so you have to type something inside single quotes. Gonna give it just one more command, inside this function and that's it. So let's run it and see what happens. Well, the first thing that happens is give me a number, buddy. Prints out, whatever you put in quotes here shows up first, and then you see this blinking bar. This blinking vertical bar at the right which says to the user, look are you going to type something or am I just gonna wait here forever? And wait it will right there in that state, probably forever until your computer burns up, blinking away until the user types something and hits enter. Of course you can stop it with a Ctrl+C, but we're gonna give it pi over two. I hit Enter, or Return. MATLAB evaluates pi over two inside the command window. Gets 1.5708 and feeds that to the function input. Input then returns that value. So it shows up right here, on the right side of the equal sign inside the function. That value, 1.5708, is then copied into x in this assignment. In the last statement in the function it adds one to 1.5708, it's 2.5708. And assigns that to A. Finally, one more returns, with the value inside A that's 2.5708, which is one more than the user typed in, hence the very clever name, One More. The sequence of characters that begins with the G in gimme and ends with the space after the colon, here to here, is a prompt. It's a little fancier than two greater than signs like down here, but it's still a prompt. Whatever prompt you want input to print for you is fine, but you have to give it a string or it'll cause an error. And what the user types down here where the blinking vertical cursor is has to be a MATLAB expression that's legal. It could be a number, a variable, a vector, a matrix, even a string, and it can involve arithmetic operators, function calls and so on. It just has to be a legal expression. Once a user hits Enter, MATLAB evaluates the expression and hands it to input. Then input returns it as an output argument. In this particular case, it's assigned to X. Note that MATLAB pays no attention at all to what's written in this prompt, regardless of the type of object that the user types in, all those things I just mentioned, it'll be accepted. The fact that the prompt tells the user to put a number in means nothing to MATLAB. It's of no consequence. A program's work is no use to anyone unless it communicates its results to the outside world. So far we've taken advantage of the fact that a command that's not followed by a semi-colon automatically has the value that it calculates printed to the command window. MATLAB provides a far more sophisticated way for printing with the, fprintf function. Here's a very simple example, which you saw at the end of lesson three. There. It prints its string to the command window. Well except for the backslash and the n, which together have a special meaning in fprintf. We'll look at that in just a minute. You can also print to a file. We'll explain the requirements for printing to a file later. For now, we'll concentrate on printing to the command window. fprintf requires at least one argument. Its first argument is a string. It's not just any string, it's called the format string because it specifies the format in which printing is to be done. Such as the number of decimal places to be used for printing numbers. The horizontal spacing, the vertical spacing, etc. It even includes the words that are to be printed along with the numbers. In this first example, the backslash and the n together, we call that a \n, specifies vertical spacing. It means, go to a new line. The n is for new line. Let's see what happens if we omit it. I'm gonna hit up arrow. Space over here and get rid of the end and hit Return. See the difference? The prompt appeared immediately after the last character in the string, right after the three. But when we had the new line specification, a new line was begun after the three, so the prompt appeared on a new line all by itself. Let's put that new line specification back in. In fact, let's put a couple of them in there. Now you see the difference? Right here after the three, we add a new line which brought us here, and then we had another new line which brought us here, and then came the prompt. Okay, that's how to print a string with fprintf, and how to make it skip to a new line. But we need to look at this function more closely, because we need to see how to print variable values with it. When you wanna do that, which is most of the time, you include the names of the variables that you wanna print as additional input arguments after the format string. We'll illustrate how it works with an example function called checkout. I've got it typed in already, saved in an M file. Here it is over here. I can load the M file simply by double-clicking on it in the current folder window. I'll do that, and here it shows up in the editor. I'll explain it in a minute but first I wanna just go ahead and run it. There. I gave it two arguments, 3 and 2.71, and it printed out three items at 2.71 each. Total equals $8.13. The numbers that I gave as arguments are passed in as input arguments, and are copied into n and price. As usual. So inside the function, n has the value 3, and price has the value 2.71. And then total is calculated by multiplying them together here. And finally fprintf is called. As in the typical case that I mentioned, there are additional input arguments to fprintf following the format string. So here's the format string, and here are the additional arguments. In this case, n, price, and total. And these three arguments contain the values that we want to be printed using the format specified in our format string here. When there are values to be printed, the format string will contain percent signs. You can see them here. There's one for each argument to be printed. First percent sign corresponds to the first argument to be printed. The second one, corresponds to the second one, the third to the third and so on. In this particular format string, there are three percent signs corresponding to the arguments. The percent sign is called an escape character. You can think of it as causing fprintf to escape temporarily from printing each character in the format string and instead doing something special. In this case, the special thing it does is print one argument. After each percent sign is a formats specifier. Formats specifier is a sequence of characters that together tell MATLAB the precise format to use when printing the corresponding argument. The format specifier always ends with a letter and there may be a number, often a decimal number, between the percent sign and that letter. For example, in this format string the first format specifier ends with a d, and the other two each end with an f. And the second and third specifiers include numbers. The letter at the end, like the f here, is called a conversion character. It's called a conversion character because it tells MATLAB how the value of the argument is to be converted to printed output. The numbers here, the 5.2 in this case, provide a bit more detail as to how the printing is done as we'll learn in a minute. This method of specifying the format of printed output, and the function fprintf itself, both come from the language C, which adapted them from earlier languages. They've since been included in many other languages as well such as C++ and Java. So if you tackle these languages after you learn MATLAB, you'll have a head start on Printout. We're going to look now to what these specifiers did. But first I'm going to grab the border between the editor window and the command window and move it up a bit so we can see everything at once. There. Let's look at the first specifier in our format string, %d, right here. The conversion character d means print using scientific notation. Except for whole numbers which will be printed without using either a decimal point or a scientific notation. Here we see that n, this n here which had the value 3, is printed as a simple 3. And here it is, 3. Because 3 is a whole number. Following this %d, is a space, and the word items, space @, space, then another percent sign. So we see this space, items, space, @, space. By the way, you can see all the possible escape characters and all the possible conversion characters by giving the command help fprintf or doc fprintf. But we're gonna give you enough information to get ya started. The next escape sequence is this %.2f here. The conversion character f means print using fixed point notations instead of scientific notation. So that's just normal decimal notation, it's good for smaller numbers. The two after the decimal point here specifies the precision. It means that exactly two digits must be included to the right of the decimal point, even if they're all zeros. If you replace that two by zero, then the fractional part and the decimal will be omitted. So we see down here the value in price which was 2.71 is printed and there are two digits to the right of the decimal point. After the one comes a space and the word each. Which were copied from the format string. In computer science terminology, the combination of the percent sign and the format specifier is a special case of an escape sequence. An escape sequence is any sequence of characters that starts with an escape character and has special meaning. Well, after the word each here, we see the by-now familiar \n. Which we've learned means start a line. Well, the backslash is another escape character. We've had the percent now, and then backslash. And the two character sequence, \n, is another escape sequence. So, after the each, we start our new line, and the next thing printed is, total space Equal, space, dollar sign. Total, space, equals, space, dollar sign. Then up here in the format string, we hit the third percent sign. The five in this decimal number here means print using at least five spaces. Since f is the format specifier, these five spaces can include a minus sign if the number is negative, some digits before the decimal place, the decimal point itself, and some digits after the decimal point. In this case, two of them. If more spaces are needed, then more will be used. Here we need only four spaces to print the value that's in total, which is 8.13. Because we have the 8, the decimal point, and the required two digits to the right of the decimal point. In a case like this when fewer spaces are needed than are specified, some blanks will be inserted before the number. In this case, just one blank is required to use up that fifth space. It may have occurred to you that we may occasionally need to print a percent sign or a backslash or a single quote. Two of these are escape characters, and the last one ends the format string, so how in the world do we print them? Well, you just put two together with no intervening space. Here I'll show you in the command window, starting with a percent sign. Here we can see that these two percent signs, printed out as one percent sign, while this single one is treated as an escape character, introducing the format specifier .3f, which specifies fixed point representation with three digits after the decimal point has happened right here. To print a backslash, use two of them with no intervening space. Backslash backslash. Like this. So here's a \ \, which printed a backslash. And we even added a new line after here, which is why we needed three backslashes. And you remember we've got one more. How about a singe quote? There. Two in a row does it again. Formatting output and F print F can be frustrating at first because there are many pitfalls. Most common pitfalls that strange things can happen if the number of percent signs doesn't equal to the number of arguments following the format string. There aren't enough arguments, and printing will stop at the first unfulfilled percent sign and the string will be truncated right there. Let's look at an example first, with four percent signs and four following arguments as they should be. That's fine, looks like the beginning of recipe for a big chocolate cake, one with a pound of butter over here. Watch what happens if we forget the last argument, that is this 4. So, now we just have 1, 8, 4 over here. There are four percent signs. This time only three values to print. So fprintf gets to the fourth percent sign. Right here, and doesn't know what to do. It prints 1 lb butter, 8 tbsp cocoa, 4 cups of flour, and prints the space and then there's nothing left. In this case, it follows the safest path. When in doubt, leave it out and it stops printing right there. There's no backslash in, so no new line. So what happens is the prompt just prints right here at the end. And what if there are more arguments than percent signs? Well, that's even more confusing. Let's put one too many. There are four percent signs. Let's add a fifth argument and see what happens. Wait, I think I need to widen the command window first. That ought to be wide enough. Let me jump down one space and there's my command. And there. 9 lb of butter, that's a lot of butter. What happens in this case the MATLAB recycles through a format string to print the extra argument. So after printing four cups of sugar here at the end of all of this. It's at the end of the string, it finds this 9. It goes all the way back to the beginning of the format string and starts all over. He uses this first percent 'd' to print the 9, prints that 9, then the space- 'lb' space, comma, space. It gets to this percent d and it's out of argument, so it stops right there. Again, it doesn't make it to the backslash end again so no new line, so the prompt shows up right here at the end. There's a reason for this recycling feature. It allows you to print a whole vector of numbers with one format specifier. Like this. Here I put the format specifier 4.1f after this percent, then a new line. And we got a vector with six numbers in it. So what's gonna happen, is this string here Is used to print one, you get to the end, we're ready for another argument. It comes back and prints again, and prints again, and prints again, and prints again. This is a special feature of the F and F function in MATLAB. Both these pitfalls, too few arguments and too many will plague anyone who prints with fprintf. I've got the scars to prove it. And there are even more pitfalls when you start printing character strings, or use more sophisticated format specifiers. On the plus side, though, when it's behaving itself, fprintf is capable of some pretty fancy output. [MUSIC]