Saturday, September 26, 2009

Matlab, String, Char and Array

Create Array of String

Creating array of String is different from creating array of character. For creating array of String, we need to use cell instead.

For example

variable1 = [];
variable1(1) = 'Dec';

The second line will give an error: "Subscripted assignment dimension mismatch" because the variable1 has 1x1 dimension, while 'Dec' will be treated as 1x3 char

For doing this we will use cell instead

variable1 = {};
variable1{1} = 'Dec';

to call it use '{}' too.

Create Array of String from Array of Char
Use cellstr(array_of_char)

For example if we have variable p, where:
>> p

p =

1985G
1985G
1985G
1985G
1985G

>>size(p)

ans =

5 5


.then

>>cellstr(p)

ans =

'1985G'
'1985G'
'1985G'
'1985G'
'1985G'

>>size(cellstr(p))

ans =

5 1