Now we will consider using fprintf to insert the contents of variables containing text into a larger text string. To begin, let's define a couple of variables here. First, numcats equals 7. Second, a text string, favcat equals floozy. If I want to print the sentence of my 7 cats, floozy is my favorite. it where the 7 is inserted from numcats and the word floozy is inserted from favecat, I can start by saying fprintf and then of my percent g.
Remember since numcats is a 7 and that's what will be inserted there, g will print it as an integer. Cats, comma. Now right here I need to insert the text string from favcat.
The special letter that I use for text format is s for string. So %s says I'm going to insert a text string. So of my however many cats, whoever.
is my favorite period backslash in glitch then my list of things that I need to insert the first thing is the number of cats so that's going to be numcats then to separate the items in the list I separate them with commas the next thing is going to be favcat close the parentheses there it is of my seven cats floozy is my favorite what we had a number of text strings stored in a cell array. So let's say cats equals cell array, floozy, cocoanut, upchuck, boom boom, doodle, Neptune, and murky. And those are actually the names of my seven cats. We've now defined that. Let's print out a slightly simpler sentence.
My cat's name is to do that. Just my cat. Now again note the double single quotes to get the apostrophe. My cat's name is insert a text string, then a new line. And here I'm going to select Boom Boom, cats, and then to pull out the contents, the text string, I need to use curly braces to extract the content.
Remember, curly for contents. This says my cat's name is Boom Boom. Boom Boom got his name because the entire house shakes when he waddles around. Here he is sleeping with the poopy dog who weighs about 60 pounds to give you an idea of just how big this cat is.
All right, let's look at one more thing before we move on. What if I simply said my cat's name is percent s backslash n and then I simply listed the name of that cell array without indicating which element. In other words, that says if everything in cats I want to print.
There are a couple of things that you might immediately think. One is, well, there are a whole bunch of things in cats, and there's only one insertion point. As it turns out, that's not really such a problem.
The other is, will it work since this is a cell array and it contains text? Will it actually pull the text out properly in this context? And the answer is no.
Note the error function, meaning f printf, is not defined for cell input. We did not specify in any way that we were extracting the contents. So it saw a cell array and it says, wait a minute, I can't print a cell array. We can fix that. I use the up arrow there.
By doing the following, we can say cats. Now to get the contents, I've got to use curly braces, but if I really wanted all of them, I want to go from one to two. use a colon and then to the last one now I happen to know there are seven elements in that cell array so I could just put a seven but if you did not know the number of elements in there then you could use the length function and say length of cats and you've got to have a closed parenthesis there and then you've got to have a closed curly brace for the cell array there. My cat's name is Flusy.
My cat's name is Coconut. My cat's name is... You see the point here.
Note what happened when there was more than one thing. First of all, since I extracted all seven elements using curly braces, they did come out as the content, which are text strings, not cell arrays. And secondly, since there were more items than I had insertion points, what MATLAB does is the following. It says, my cat's name is insert value.
What am I inserting? I'm inserting floozy. So it inserts floozy.
But then it says, wait, I've got another one. I've got coconut, but I'm out of insertion points. So let's just recycle that text string.
My cat's name is insert coconut. New line. Whoops, still got more.
My cat's name is upchuck. Whoops, still got more. My cat's name is boom boom. And so forth. It will reuse this text string until it.
it manages to print all of the things in the list. Sometimes that can be very confusing, as we will see in the next presentation, which involves making tables with fprintf.