MATLAB: Problem with matrix composed of numbers and strings

matrix

I ran into a problem where for some reason when I try to create a matrix with a string and a vector of numbers of the same size only the strings show up, for example: Any explanation would be much appreciated.
>> a='hello'
a =
hello
>> b=1:length(a)
b =
1 2 3 4 5
>> c=[a;b]
c =
hello

Best Answer

Matrices cannot be composed of a combination of character and numeric information. They must be character, or they must be numeric. When you used vertcat here, MATLAB converted the numeric array into its ascii equivalent, using the ascii tables.
c = [a;b]
c =
hello
whos c
Name Size Bytes Class Attributes
c 2x5 20 char
So what happened? c is a character array, of size 2x5. But that second row of characters were unprintable. So you just see white space when you display the matrix c.
But the position of the character 1 in the ascii table is 49. So when MATLAB tries to convert 1 using the ascii tables, it converts to an unprintable character, NOT the character '1'.
+'1'
ans =
49
If you REALLY wanted to combine the two, into a readable array, then try this:
c = [a;b+48]
c =
hello
12345
Alternatively, you can use a cell array, but it won't be terribly readable as a composite array as I think you want to see.