# Strings in Matlab, its types and uses explained with Example codes

**Strings in MATLAB:**

**Strings in matlab:-** A MATLAB string is an array of type char. A string in computing language and in MATLAB language as well as a sequence of characters generally we think of a string as text as opposed to numbers and numerical data which we have been using a lot of a course in our MATLAB. So far, we have seen strings in several places though for example when we do plots and we ask the MATLAB to put a title on our plot we use the title command and inside the parentheses for that command we use to enter some text enclosed in single quotes and the text that we enter in the single quote is considered a string. This is a lot different than numerical data in a number of ways and being able to work with strings is just as important in MATLAB as it is to be able to work with numerical data. So let’s discuss about strings for a little bit and how MATLAB handles them. Strings can be stored as variables and we usually enter in the string value as in within single quote marks for example I can set up the variable name equal to my first name single quote ‘electronic clinic’ and the single quote hit enter it stores electronic clinic in the variable name.

name = ‘electronic clinic’

Now look over here in the workspace and you see something a little bit different list of the variable name but here instead of having a little grid a little tiny yellow grid here we have a yellow square with ch on it so this is telling us that MATLAB is storing this variable as a string value and not as a number value.

It is one important way to be able to tell what kind of variable you have by looking at its icon in the workspace. I can also enter in something like greeting equals again single quotes hello world so I can enter in longer strings and also store those variables.

greeting = ‘Hello world’

This is a little tricky I can even enter in numbers but instead of making MATLAB treated like a number I can make MATLAB treated as astring for example if I enter in 42 enclosed in single quotes it is going to store it not as the number 42 but as astring of text.

num = ’42’

Again this is different than if I said x equals 42

X = 42

Notice over here in the workspace now 42 is listed as a numerical value.

Hence the icon here so again notice that num variable is not a number. If I go to do something a numerical with it

num + 1

For example it is not going to give me what I expected to get which would have been 43 you would think but since 42 in the variable num is a string value that’s doing something some what different what exactly is that doing well one thing you might want to know about how computers store information is, a computer store string information every character that you see on your keyboard as a numerical value but in a special kind of way, uses a numerical representation called ASCII. ASCII stands for **American Standard for code information interchange** and under ASCII every character on your keyboard and even some of the character of the elements of your keyboard that don’t have characters before them have numbers that correspond to them and we can find out what those numbers are using the command for example I have to put that in quotes double of capital a is 65.

double (‘A’)

So you might type capital A and enter it in as a string but the computer sees the number 65 underneath all that and so even numbers entered in as string values have their own ASCII values for example double of the number four is 52.

double(‘4’)

Now it makes a little more sense when you think about what’s happening when I take num + 1 it’s taking the ASCII value for four which is 52 and adding one to it to get 53 and then it’s also taking the ASCII value for two which is 50 and any one to yet so that may be a little bit more information that we need for right now but just realize the upshot of this is that strings and numbers are very different and they are treated very differently in MATLAB. Now you might notice when I calculated that num + 1 actually gave me two results the answer there up here was actually a vector and that brings me to the one thing that you do need to know definitely; for now about strings is that just like everything else in MATLAB, strings are treated as vectors. For example, when I take name that was **electronic clinic** MATLAB treats that as a six entry vector the first entry which is the capital E the second entry is l and so on. This makes Strings awfully easy to work with for example I can type name parentheses one and it’s going to return the first element in the vector.

name (1)

It works just like the referencing a single element and a vector I can also use a slicer and just take the second through fifth letter that name as you see here.

name (2 : 5)

I can even put them into a larger vector for example:

v = [greeting name]

It will make a little greeting for me it just concatenates them puts those two strings next to each other.

Each character is stored in 2 byte of memory for example if we write:

str = ‘this is a test’

Then write the command

whos

**String commands:**

**isletter command:**

So the first function that I am going to discuss about is **isletter** so isletter looks at a string and tells you whether a character inside of the string is a letter or not by giving you a logic 1 or 0. It will give 1 if there is letter in the string and 0 in case of numeric value. For example if we write the command:

isletter (‘123 electronic’)

then it will gives us 0 in case of numeric and 1 in case of alphabet such that:

0001111111111

**isspace command:**

Now another function we also have something called isspace which is an interesting one what it does is pretty simple just like letter it will gives us 1 in string where we have used space. This isspace looks at a string and tells you if there is a space by giving you a logical 1 or 0. For example we write the command 123 then give space and write 45a.

Now we can see that after 123 we give space due to which at output we get 1 and on other values we get 0. You see there’s a 1 in the middle which is our space so that was simple.

**ischar command:**

ischar what it does is basically it looks at something and tells you whether it’s a string or not what do you mean well I mean this for example if we write this command and if inside the command we have string then it will gives 1 otherwise it will give 0. For example:

ischar (‘123’)

Now in that case we have given a string due to which we get 1 at the output.

Now if we give some numerical value without commas such that:

Now in that case we will get 0 because we have not given the commas.

**num2str command:**

Now using this command we can convert the integer into string. For example we want to convert 3.1415926 in to string.

num2str (3.1415926)

We have got only four decimals what happened to the rest while default gives us only four decimals but we can change it like this so if you press the up key it is going to give you the same number

Now put a comma with the number and indicate how many digits you want let’s consider I want six so I will write six

num2str (3.1415926 , 6)

It is not exactly the decimal as you figure it out it is the total number we can also make it like two and we will get two points.

num2str (3.1415926 , 2)

**Str2num command:**

We also have it the opposite way what does that mean it we are turning a string into a number and we also have str to double so what it does is pretty simple let’s say I have something like this:

str2num (’66 10 170′)

Now the above string is converted in to number we can add it for example:

sum (ans)