views:

46

answers:

1

I would like to make a list of strings in MATLAB using the example below:

x = ['fun', 'today', 'sunny']

I want to be able to call x(1) and have it return 'fun', but instead I keep getting 'f'.

Also, is there a way to add a string to a list without getting the list giving back a number where the string should be? I have tried using str2double and a few other things. It seems like both of these thing should be possible to do in MATLAB.

+4  A: 

The easiest way to store a list of strings that have different lengths is to use cell arrays. For example:

>> x = {'fun', 'today', 'sunny'};  %# Create a cell array of strings
>> x{1}                            %# Get the string from the first cell

ans =

fun
gnovice