Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a cell array of strings containing numbers

Tags:

string

matlab

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.

like image 432
Kena Avatar asked Jun 04 '10 18:06

Kena


1 Answers

I know it's been 4 years but I came across the undocumented function sprintfc which is used to do exactly what you want:

CellArray = sprintfc('series %i',1:3);

CellArray = 

    'series 1'    'series 2'    'series 3'

well... it might be useful to someone I guess.

like image 133
Benoit_11 Avatar answered Oct 05 '22 22:10

Benoit_11