The rest of this lesson, we're done with cells, and we're done with 2015, which is the year that I recorded the part of this lesson that you've watched so far. We're done with that old 2015 microphone. I guess I need to explain all of this. I've traveled forward in time again, and I'm now speaking to you from July of 2018 with a brand new microphone that makes me sound even better, if you can imagine that. If you watched the videos in the first lesson, which were added in March 2018, and you've already heard this more mature mic as recorded by this less mature mic. Get it? I'm a mic, and my microphone is also a mic. See, I'm older and it's younger. Man, I just crack myself up sometimes. Anyway, in that segment, we showed you the MATLAB desktops for both MATLAB Online and installed MATLAB. MATLAB Online hadn't even been invented yet back in 2015, so I had to jump forward in time in order to be able to use it. Now, I've gone back to the future again so I can tell you about some new data types introduced by MATLAB relatively recently. No matter when you're watching me, this course has already covered all the datatypes that have been part of MATLAB since back in the '90s. The double and single floating point numbers, all the integer types like int eight, unsigned, int 16, et cetera, et cetera, and the char, and the logical, and the struct, and the cell, which we just now finished with. But just now, I'm in the segment you just watched, which was recorded three years ago. But in the next segments, we're going to look at some new data types. If you're using an older installed version of MATLAB, you may not have these types. But if you're using MATLAB Online, then of course, you'll definitely have them, because MATLAB Online is always right up to date. Speaking of being up to date, I'm using the very latest version of MATLAB, which today is, as you can see up here, 2018a, which is exactly the same version that I was using when I introduced the desktops in the first lesson. That a, in the name means that it's the first version released in 2018. MathWorks has for many years released two versions of MATLAB per year. The first one shows up in March and is labeled with an a, and second one shows up six months later in September, and is labeled with, you guessed it, a b. The changes are usually not big, so you can stick with one version for a year or two or even three, and you'll still be fine. We'll add one new video segment for each new data type that we think you should learn about. You can look at the title of each video to see the name of the type and the version of MATLAB in which it was introduced. The segment that we're in now, for example, is entitled the String type introduced in 2017a. You may wonder how the String type, which we are introducing now, relates to strings, which is the title of an earlier segment in this lesson. We'll talk about that relationship in great detail in a few minutes. But a quick overview is that both of these types are used to hold words, phrases, sentences, et cetera. But as we pointed out in the earlier segment, the type of the strings, in that earlier segment was char, not string. There are some problems with using char to hold string, and MATLAB introduced a new type with the name String to solve them. We just finished using the old string right there near the end of our segment on cells when we form the word strawberries. Let's get some more strawberries. In this command, we've assigned the string strawberries to the variable name fruit. Let's determine the type and dimensions of fruit by using the class function and then the size function. So, the function class tells us that the type of this variable is not staring at all, the type is char, as you can see right here. The name string is just an informal way of saying a vector of char, as each of which is printed on the screen as a character, S-T-R-A-W-B et cetera. The size function reminds us that the so-called string is a row vector, just like any set of characters that we enclosed in single quotes. This particular one has 12 elements. I know you remember all this char stuff and are probably edging closer and closer toward an epic case of total boredom. But before you drift off to sleep, I just can't help but also point out, that when the function class return the type of fruit right here, it did it with an output argument whose type is also char. We know that because the word char surrounded by single quotes. Okay. Hello? Sorry about that, but you should probably wake back up now because here comes the new string type. To get it, we can simply use double quotes instead of single quotes, like this. So there you go. You are looking at a new string. You can tell it's the new string because it's surrounded by double quotes instead of single quotes where the old string was. There is something remarkable about these double quotes. Other languages like C, C++, and Java have used double quotes to denote a string for decades, but double quotes had to wait 40 years to be given a place in MATLAB. Congratulations, dequeue, you're in the game, and you're looking good. Note that our new string type gets the same purplish pink color that we got when we typed it in a char. So there we are. We now have our first new string or to put it another way, we have our first string. I'm not always going to say new string for two reasons. Number one, you can figure it out from the context whether I'm talking about a string that is a char vector or a new string. Number two, I'm tired of saying new string. If we need to distinguish, I'll call the old version of char vector or char string or just a char. Otherwise, string means new string. To prove that new fruit is in fact a string instead of a char, let's check it with the class function. Sure enough, it's a string, but the class function still gives it's answer in the form of a char vector no matter what type of input it gets. We'll talk about differences between chars and strings in a minute or two, but first, let's talk about a similarity. Probably the most important similarity is that almost any built-in function that's designed to process a char vector, will process a string in the same way. The prime example is good old fprintf. As you can see from these two commands, it's happy with a format argument, that is either a char vector or a string. Then, there are all the built-in functions that were designed originally to manipulate char vectors. They're also happy with the string. Let's put one of those happy little functions right here in the command window. Remember the function that we used earlier in this lesson to search for a substring inside another string? It returns all the starting positions of the substring everywhere it finds it. The name is string find abbreviated for the function name as S-T-R-F-I-N-D. We'll first make an old char string. And then we'll call string find to find something in it. Let's say, inane_ refrain = 'The rain in Spain stays mainly in the plain.' So, we've got the string to search in, and we're going to search for, I don't know, the string in like this. There. String find happily searched through the string in the first argument looking for the substring in, which was the second argument. He found it in six places and gave us the index for each one of them. Now, maybe we want to have another happy little string find function call. Let's do something different with this one. Let's give it a string for the first argument instead of a char vector, and use the same second argument. Little equals bushy headed trainer in an art form mainly planar. There. As you can see, our happy little string find function is just as happy with the string as it was with a char, and I could have used a string for the second argument too. By the way, if you need to look for more complex string patterns like, for example every substring that starts with a lowercase a followed by one or more lowercase letters, followed by one lowercase r, then you should look up regular expressions and the built-in function that searches for them named REG EXP, which stands for regular expression. It'll take you a while to get used to it and by a while, I mean days. But here's how to tell it to find that substring example. Take my word for it, it found it three places. I could show you lots of examples of such string functions that were originally designed for char vectors, but would also work for strings. But for now, I'll just show you that the function upper works fine with both types and let it go with that. Of course, the function lower works just as well. So, all the built-in functions have now been tweaked by the MathWorks programmers, so that they do their things with either a char or a string. In other words as we've learned, that means that they're all polymorphic functions. Since char and string are different types, these functions have to do different things internally with them, but that's the beauty of the concept of the polymorphic function. You just give it the argument and the function looks at the type and figures out what to do. That's the best way to write any function that might be appropriate for use with more than one type if you have the time. But let's suppose that you've already written a whole bunch of functions that require an old-fashioned char vector instead of a string, and you don't want to or you don't have time to change all of them and then handle strings too. Well, never fear. The converter is here. Before we find out what that means, let's suppose you've written a function that reverses a row of chars just like this one over here, called my STR function, which is supposed to mean my string function. Here's what the function looks like. It's just a one liner. It takes the input and then it just plays it backwards for you. Let's suppose you want to reverse strawberries. Let's first see what happens when we input strawberries as a row of chars. That's what we assigned to the variable fruit, so let's use that as an input. I think I'll clear this cluttered command window first, and let's move this command window underneath the editor so we've got a little bit more space. Here we go. Clean slate. Alright, now let's try out our function on the char vectors strawberries. Oh, that looks kind of disgusting for some reason, but the function is obviously working because the variable fruit contain the word strawberries and seirrebwarts is strawberries spelled backwards. Now let's give it the variable new fruit, which contains the word strawberries as a string instead of a char vector. It didn't do anything. I'll let you figure out why, but I'll give you one hint if you need it. Think about how many elements there are in the input variable new fruit. There's only one. A string, no matter how many characters it contains, is just a single element. So, how do we get this function to reverse a string? Well, we can convert the string to a char like this using the function char. Then we can use this char vector as the input, which gives us char vector as output and we can convert that output to a string. We need a little more room. Okay. That worked, but it requires two new variables and three command lines. Well, we can do all this with no new variables and only one line like this. So here, we've converted new fruit to a char, and here after the function has returned its value, we've returned the output of the function to a string. Is that slick or what? We didn't monkey around inside the function itself. Instead, we just switched to char on the way in and switched to string on the way out. I think you've got the picture, but I want to do one more example of converting a char to a string. This example reminds us that in order to insert a single quote into a row of chars, you have to include two single quotes back to back and reminds me of one of the greatest Broadway musicals of all time westside story. Just thinking of this poignant song gives me goose bumps. I could sing the song for you and you'd probably get goose bumps too. Well, the kinds that you get when your skin crawls. But that would be the end of my video career for Vanderbilt, so I'm not going to do that. What I want to point out from this example is that the same sort of doubling requirement shows up for strings. In order to insert a double quote into a string, you have to include two double quotes back-to-back as in this example. Some people make your skin crawl for a living. Any circus is amazing but as you can see, I had to put a pair of double quotes around my precious to end up with just single double quotes. By the way, you might have noticed that there's a double comma too, but unlike double double quotes, the commas come through doubled in the string itself. That just shows that this double to single business doesn't happen with any other punctuation marks or any other characters for that matter. Don't I have quite the knack for making simple things sound complicated? Maybe that means I'm a really bad teacher, or maybe I'm cleverly making it sound like you're learning really hard stuff so you can impress your friends. Of course, it's not really hard at all, but the single versus double-quotes business is the most obvious difference between the two types. This is a syntactical difference between them, and syntax is usually much easier to understand than semantics, as the semantics of the one line string reversing example above shows. There are other semantic differences that we need to look at of course. One nice difference is that the string type does not suffer from a particularly irritating limitation of the char type which we ran into in lesson seven. The limitation occurred when we tried to put more than one row of characters in a two-dimensional array of chars. Before we try that again with a char version of strings, which will fail as miserably now as it did then, let's see how easy it is to do it with the new string type. Here, three-by-one string array means that there are three rows, one, two, three, with one string on each row. Regardless of the number of characters in a string, it's just one element. Since there's just one element on each row, this is the column vector. Again, now I'm going to try to make a stack with a char version by using single quotes instead of double. Is that red stuff. Dimensions of arrays being concatenated are not consistent. We saw this same error earlier in this lesson. The error was worded slightly different in the 2014B MATLAB that I was using then, but the problem was exactly the same. That problem is that in the second command, this one, we are trying to make an array whose rows do not all have the same width. Whereas in the first command, the 'ace' on the first row is just one single element. The second command, 'ace' comprises the three elements a, c, e, and in the first command 'king' and queen on the second and third rows, are also each just one element, whereas in the second command, 'king' on the second row has four elements, and queen on the third row has five. MATLAB never allows different numbers of elements on different rows of the same array, as we first pointed out in lesson two. Arrays are rectangles, meaning that all of their rows have exactly the same width, and I can guarantee you that until that sad day when MATLAB finally ceases to exist which by the way is over 200 years from now, it will continue to enforce that rule. What? How do I know it's over 200 years? You've forgotten I got a time machine? I know many things, many things my friends. This rule stretching forth over the centuries as it does, has nothing to do with chars or strings in particular, it's a basic rule of arrays and all the widely used computer languages enforce this same rule. As we saw back there in lesson two, you'll get the same error if you try to sneak around and put different sized rows into an array of doubles instead of chars. Let's see, I don't know, let's just do this. There, the first row has three elements, the second row has four and the third row has five. Now, let's hit "Enter." No go. Different type, same rule, same error. If you really need a stack of different lengths strings of type char, you can use this handy little trick. When you give a column of strings as an input to the function char, it not only changes the type to char, but also adds just exactly the right number of blank characters, the right of each short row to make all the rows the same width as the longest row. As you can see by the space between 'ace' and it's closed in quote mark and by the space between 'king' and it's closing quote mark. So, it returns a rectangular two-dimensional array of type char. All strictly legal. We put strings on top of one another, but what happens if we put them side by side? Let's clear the command window and do that and we'll do it with strings first. No surprise there, we put in three strings in a row, and got out a row with three strings, which is as MATLAB says, right here a one by three string array. Now, let's try the same thing with the char version. So, what happened here? Why do all the letters get shoved together into a single string of chars? Well, this same thing happens anytime you put multiple row vectors on the same row. You end up with a single row vector containing the elements of the multiple row vectors one immediately after the other. Works the same way with numbers, of course. Here's an example of that. When you combine multiple strings together to produce one string, you're concatenating them which we've talked about before. After strings are concatenated, you can't tell where one ends and the next one begins, but sometimes concatenating strings is exactly what you need to do. So, how can you concatenate strings? Well, you just add them. By add I mean use the plus sign, which we've used so far only to sum up numbers. Here's how it works with strings. Hey, I wonder what happens if we use a minus sign. Let's try that. Wow, how about that? MATLAB figured out that these strings are numbers and then just kidding, I've faked this one. Here's what really happened, and you'd get an error like this with any other arithmetic operator too. The plus sign is the only arithmetic operator you can use with strings. Enough with concatenation, let's clear all that away and get back to conversions. We've converted between strings and chars, but what about converting between strings and other types? Well, for openers, you can convert any number to a string. First, let's look at four examples with numbers of the type double, which is the default number type in MATLAB, and here's a fifth example in which we convert from one of the integer types to a string. This works for all the other number types too. These examples came at you pretty fast. Let's examine them a little. The second and fourth examples, here and here, show that only four digits after the decimal place are given in the string. This is MATLAB's short precision which is the default in the command window. The third example shows that the string function also uses the default number format of the command window, which is fixed point instead of floating point which was the form of the input for this example, as you can see here. The fourth example might seem special because the input was pi. Remember that the input argument pi is a function that returns a double, and like any input argument to any function it's evaluated before it is passed to the function string. When it was evaluated it returned a double, so string did it what it did for the first three examples. It just changed a double into a string. The string function can even convert from a logical. Look at this. Here's a really nifty example. Oh, my. Looks like I made a little bit of an error here, but MATLAB is helping me out. Yes, MATLAB I did mean that. Thank you for adding that parenthesis for me. What a great system these MATLAB program has given us? But anyway, why did we get this vector of four strings? Well, you may remember that the greater than sign operates element by element and it returns a row vector of logicals. String, was given that row vector as an input and it converted each element to the appropriate string to produce a row vector of corresponding elements. So, to summarize conversion from logical to string, a true input produces the string true and a false input produces the string false. Not exactly earth-shaking, but this operator can be quite helpful and you need to produce output in string format, say for printing or for exporting to Excel or any other application that takes strings. You can't go the other way though. As we learned when we tried to do subtraction on numbers written as strings, MATLAB will never try to determine the meaning of words and strings. But you maybe surprised to learn that it will determine the meaning of a numeric string. Here's some examples. By the way, only double is capable of this trick reading numbers and strings, you can't do this with other number types like int, you, int, et cetera. There are some important differences from what happened when we convert it in the other direction. Let's pop that one up again from the command history for comparison. The only difference that you can see here, is in the fourth example. When we converted from the string pi to a double up here, we get NaN. But when we converted from the number pi to a string, we got 3.1416. So, why did we get NaN instead of 3.1416? Well, NaN means not a number, and this is the function doubles way of enforcing MATLAB's rule that it will never try to determine the meaning of a word in strings. When you give it a non-numeric string, it returns NaN every time. There's another difference that you can't see. You may remember that for converting from a double to a string, the format short is used. So, here. Well, when we're converting from a string to a double, the format is whatever format that is said in the command window at that time. It's currently set to short, but watch what happens if I change the format to long and repeat this command up here. We make this window a little bit bigger, so you can see what's going on. So, what you want to compare, is this command and this identical command. Look at this output. This number we got 1.1235, because at that point, the format in the command window was short. Now, we get all these extra decimal places, and I'm going to take it back to short because that's why I like. Before we continue, we should check to see what happens when we give double the old char type of string instead of the new string. Here's a head-to-head comparison string first char second. We've already seen the first one which works perfectly, but what happened in the second command. Yeah, we talked about this earlier, but in case you've forgotten, let me go over it again. The argument to double in the second command is actually a vector that contains two numbers that are codes for the two characters that we call one and seven. So, with the char argument, double gets a two-element vector of numbers and it converts each of these numbers individually from char to double. It's quite a different thing from what double does with the new type of string, in that case, the string is treated as a single element, and that single element is translated into the single number 17, right here. We showed you a way earlier in this lesson to convert the entire char vector into a number with the function string to num, which is abbreviated STR2NUM. It converts the char vector properly into a scalar double. But I want to show you a new function for doing that called string to double. Its abbreviated STR, I'll just type it there. String the double has the advantage that it also works with the string input, you can't do that with string to num. This function gives you a way to write code that includes a string to double conversion when you don't know whether you're going to get a char type or a string, like this. There, it ran on and returns a random number greater than zero, then unknown will be a char vector. Otherwise, unknown will be a string, and we don't know which one it is, but we don't care because string to double works with either one of them just fine. So, that's great, but I have trouble remembering the names volleys functions, is it string to num? Or string to double? Or a string to number? Or, I can't remember all that. So, if you're like me, you just do this. There, string converts unknown to a string if it happened to be a char, and then we're handing double a string and everything's fine. Of course, were making MATLAB run two functions instead of just one. So, we might be making a little hard on MATLAB, but that's a lot easier on us, and that's as it should be. We are after all the masters and MATLAB is our obedient slave. Sometimes though, MATLAB is not so obedient, like when I ask it to convert a string to an integer type, which is I already warned you doesn't work. Conversion to int8 from string is not possible. To that, I say, "Poppycock, dalder dash and Pysch posh." If you're watching this sometime in the future with a MATLAB releases later than 2018, and you might not get this error because this conversion is definitely possible, and all the red print and the world won't convince me otherwise because I can prove it, My a proof goes like this, MATLAB can convert from a string to a double, and MATLAB can also convert from a double to an int8. So, invoking the principle of transitivity, we know that it can convert from a string to an int8. In fact, we can do it indirectly in one line like this, end of proof, QED, case closed, and more importantly, I managed to work in the word transitivity, which also shows how smart I am. So, that's it for convergence involving strings. No, wait, there's one more thing. Look at this conversion. Here, we've given the string function a cell vector containing a double, a logical, and a char vector, and it converts each one of them and returns them in a vector of strings. This example shows that the string function will reach inside a cell array and convert different types into strings. This feature lets you avoid having to use looping when you're given lots of heterogeneous data in a cell array, and you need to translate it into strings. So, that's it for conversions involving strings and this time I'm mean it. So, what's next? So, far we've treated our new strings as indivisible elements, but they of course comprise a sequence of individual characters. So, what if we need to access those individual characters? We learned earlier in this lesson that with the old char strings, we could use normal vector indexing. First, let me clear this command window. Remember, this example. Now, was Plan B for the title of our textbook. So here, we've asked for elements one, three, six of this string and that gave us MATLAB. When we first did this, it seemed a little surprising that it even worked because MATLAB for Smarties doesn't look at all like any vector we are used to. Well, now you may be comfortable thinking of a string as a vector, but don't get too comfy with this idea because it doesn't work with the new string. It doesn't work because the new string is just one monolithic element, and you can't index into an element to isolate individual characters anymore than you can index into a double to get an individual digit. I mean, like this for example. This isn't going to give us the seven. There's only one element. We asked for element two, so index exceeds array bounds makes sense, just as it did when we had a string with just one element. But you can do it another way with strings. Here's an example showing you how using a new function called extractBetween. As you can see, I've given this function three arguments: the string that I want to access, starting position of the characters that I want, and the ending position. Note that the B in the name of this function has to be uppercase, and also note that the word Between is not quite right. Because in addition to the characters between three and eight, which is this first D and this D, it includes those two characters too, so it's not really between. I have trouble remembering the name of this function, the capital B and the meaning of between. Oh my goodness, this is all way too complicated for old Mikey boy here. Fortunately, there's a work around, just convert to a char, and use normal indexing and convert right back to the string again. Sure, it takes two steps but you don't have to remember the name of the new function or how it works. I'm sure some people at the MathWorks think I'm wrong to teach this method, but I love it. If loving it is wrong, I don't want to be right. Here's another example. This stuff is tasty. I'm going to access some characters inside it. Oops, I guess I should have left semicolon off, but when you do it, you can leave it off and see the answer to the riddle I gave you 30 minutes ago. Wait, you are doing everything I'm doing, right? Don't be that guy standing over at the side in the yoga class. Again, I like converting to a char vector and indexing better than using extractBetween. But the function extractBetween does other things too, such as extract the part of a string that comes between two substrings inside it. As in this example, this works pretty well, and this time, the word between in the name of the function is correct. So, I'll be the first to admit, it extract between does have its place, and so do it's sisters' functions, extract before and extract after. Yeah, there are three extract functions. There are a couple of insert functions too, and by now, I'm sure you're guessing that there are lots more built-in functions for working with strings and or char vectors. Yep, you're right, 58 of them, 58 functions that MathWorks considers to be the basic functions for working with this data type, and we better get busy because we need to go over every single one of them in detail. So, get comfortable because, just kidding, that is not happening in my class. Instead, I'll show you where you can find a list of these functions with links to explanations for every one of them. You can figure them out yourself. I know this because you've made it to lesson seven. I can see also that you happened to be, let me check here. Yeah, you're in the top five percent of the students taking this course. So for you, this should be a walk in the park. Okay, time to clear the command window again and then I'm going to type Doc, and up pops this Help window with a document page open, that's called Matlab Documentation. To find those links to all 58 string and char functions, here's what you can do. Click on "MATLAB," which opens a document page called MATLAB. Click on "Language Fundamentals" and just to show you that I can, I'll click on this link on the left over here. These are just links without all the explanatory text. Both sets of links, the ones on the left and the ones on the right, take you to the same places. There we go, that closes the MATLAB page, and opens up the Language Fundamentals page. Click "Data Types," and now we're looking at the Data Types page. Finally, click "Characters and Strings." Okay, here it is at last, the list we were looking for, the list of built-in functions for working with characters and strings. Wait, you know what? Before I get into the functions for characters and strings, I want to show you that there are other lists of built-in functions that you might want to consult from time to time. For example, let's go back to the previous page by clicking this left arrow up here at the upper left, and that takes us back to the list we were looking at before. Then, let's click "Numeric Types," and here's a list of functions that work with numeric types. You can scroll down and see all of them. These are functions that are deemed to be basic numeric functions in MATLAB. So the list leaves out square root, for example, in cosine and hundreds of other built-in functions that also work with numeric types but aren't considered to be basic. Okay, let's go back to characters and strings by clicking the left arrow, and then clicking "Characters and Strings," and here we are again. The functions in this particular list are hidden from view right now, all you can see are subcategories. That's nice and organized, but if you want to see a list of all the character in string functions at once, just click this teeny tiny "expand all" over here at the right side, and there they all are, and we can scroll down. Here's extractBetween, which we've just introduced. I'll click on that and up pops the help page for that function. Now, you can see all the ways that you can use extractBetween, and this is exactly the same help page that you'd get if you typed "Doc extractBetween" in the command window. Sorry, what? Why did I do it that long way? What do you mean by long? A long way. I see, got it. Why did I go through all those other doc pages when I could have gotten here in one command? See, just asking me that question proves that you're in the top five percent. The greatest contributions to human thought or questions are not answers. Answers are literally in second place, and here's the answer to your excellent question. It's the only way to find what you can do when you can't remember the name of the function that does it or never even heard of the function. What we've just shown you about navigating these documentation tabs, will get you to any basic function in MATLAB for any data type. Okay, let's tidy up, because we are done with our introduction to the new string type, and we need to get ready for another new type.