Alec Motazedi
Alec Motazedi

Reputation: 23

How do I make a column vector into an array of strings?

How would one go about taking a column vector like this:

a = [1;2;3;4;5;6;7;8;9;]

and turning it into this:

'1','2','3','4','5','6','7','8','9'

Upvotes: 2

Views: 48

Answers (2)

Mike Boedigheimer
Mike Boedigheimer

Reputation: 31

If you want to use documented functions one possibility is:

a = 1:9;
t = textscan(sprintf('%d\n', a ), '%s', 'delimiter', '\n');
t = t{1}';

ans = 
    '1'    '2'    '3'    '4'    '5'    '6'    '7'    '8'    '9'

Upvotes: 0

Divakar
Divakar

Reputation: 221574

You can use undocumented built-in function sprintfc to convert a numeric array to a cell array of strings like so -

sprintfc('%d',a)

Sample run -

>> a = [1;2;3;34;5;6;7;8;19;];
>> sprintfc('%d',a)
ans = 
    '1'
    '2'
    '3'
    '34'
    '5'
    '6'
    '7'
    '8'
    '19'

As an alternative, you can also use a combination of num2str, cellstr & strtrim -

strtrim(cellstr(num2str(a)))

Upvotes: 3

Related Questions