views:

52

answers:

2

How would you efficiently build a cell array of strings which contain numbers (in my particular case, a cell array of labels for a legend).

Eg:{'series 1', 'series 2', 'series 3'}

I've tried things along the lines of

sprintf('series %i', {1:10})

but apparently sprintf and cell arrays don't play nice together.

Something like this works if I only want the number, but doesn't work if I want text as well.

cellstr(int2str([1:10]'))

Obviously, it can be done in a loop, but there must be a clever one-liner way of doing this.

+3  A: 

The functions INT2STR and STRCAT do the trick:

>> cellArray = strcat({'series '},int2str((1:3).')).'

cellArray = 

    'series 1'    'series 2'    'series 3'
gnovice
+1  A: 

A slightly different way:

cellArray = cellstr( num2str((1:3)', 'series %d') )

or alternatively

cellArray = strcat( num2str((1:3)', 'series %d'), {} )

with the result:

cellArray = 
    'series 1'
    'series 2'
    'series 3'
Amro

related questions