So what is the difference between a character array and a string? Well, it would be correct to say that a string is wrapped in double quotes and a character array is wrapped in single quotes, but there is more to it!
You can think of a string as a single piece that is unable to be separated where a character array is a combination of pieces that can be separated. Let's look at an example of this to see what I mean.
matlab
str = "string";
char = 'character';
length(str); % output: 1
length(char); % output: 9
We can use a new MATLAB function called "length()" to output the length of a variable. From the code block above, it can be seen that "str" has a length of 1 and "char" has a length of 9. This is what I meant before where MATLAB process and sees a string as 1 single piece whereas it sees character arrays as an array of characters, each character adding another increment of 1 to the total length.
We will talk about arrays in much more detail in a later topic.
Just as numbers could be added, the same can be done with strings! Let's try it.
matlab
str1 = "Hello ";
str2 = "World!";
str1 + str2; % output: "Hello World!"
Cool! Now, can we subtract strings? Let's give it a try.
matlab
str1 - str2;
% This will unfortunately error.
% There is no supported operand for strings besides addition.
Unfortunately, we cannot subtract strings, in fact, we can only add them. This is still an incredibly handy feature to implement, especially for validating code results.
Now that we know strings are able to be added, what about character arrays?
matlab
char1 = 'Hello';
char2 = 'World';
char1 + char2; % output: 159 212 222 216 211
Well, that is peculiar, isn't it. Remember how earlier we said MATLAB sees character arrays as a collection of pieces where each letter/number/symbol is a single piece. When we add, subtract, multiply, or divide these pieces, MATLAB converts them to ASCII values. ASCII values are numerical representations for characters and each letter, number, and symbol has a different ASCII value. You can read more about ASCII values here.
Above is a list of the ASCII values that we get from alphanumeric characters and some symbols. As we can see, "H" has the ASCII value of 72 and "W" has the ASCII value of 87, and 72 + 87 = 159, same as what we got in the previous code snippet!
Now try subtracting some character arrays and see what happens. I should note, they must be the same length in order to add or subtract them; this is because addition and subtraction are element-wise operations, meaning each element in the same position of the array is added or subtracted together. We can also multiply and divide character arrays together. However, in order to do so, we need to go over some array-related tips and tricks.
First, an array is an one-dimensional collection of information, just like a character array is! Secondly, when we use the multiplication (*) operator on arrays, MATLAB wants to do what is called matrix multiplication. We will talk about this much more in the array and matrix topics, however, there is a way around this. For our purpose, we want to do element-wise multiplication, so we can use the dot operator with the multiplication operator (.*). This tells MATLAB that we want to do element-wise multiplication and not matrix multiplication. The same applies for division where element-wise division can be done by using the (./) operator.
matlab
char1 = 'goal'; % ASCII values of: 103 111 97 108
char2 = 'hats'; % ASCII values of: 104 97 116 115
char1 .* char2; % output: 10712 10767 11252 12420
char1 ./ char2; % output: 0.9904 1.1443 0.8362 0.9391