7.7) Strings And Character Arrays
As we know, a string is a sequence of characters enclosed within single quotes. It is a row vector of characters (of class char).
More generally, a character array is any array of single characters. If it is a row vector (more strictly: a 2-D array whose first dimension is 1), then it is a string.
[matlab]
>> str='Hello World';
>> whos str
Name Size Bytes Class
str 1x11 22 char
>> char_array=['Hello' ; ''World'];
>> whos char_array
Name Size Bytes Class
char_Array 2x5 20 char
[/matlab]
- We are unable to make a character array if the strings are of unequal sizes.
- Instead we must make use of a Cell array
[matlab]
>> char_matrix=['Hello ; 'Worlds'];
Error using ==> vertcat
CAT arguments dimensions not consistent.
[/matlab]
[matlab]
>> char_cellarray={'Hello' ; 'Worlds'}
>> whos char_cellarray
Name Size Bytes Class
char_cellarray 2x1 246 cell
[/matlab]