How can I convert [12 25 34 466 55] to an array of strings ['12' '25' '34' '466' '55']? The conversion functions I know convert that array to one string representing the entire array.
5 Answers
Using arrayfun together with num2str would work:
>> A = [12 25 34 466 55]
A =
12 25 34 466 55
>> arrayfun(@num2str, A, 'UniformOutput', false)
ans =
'12' '25' '34' '466' '55'
1 Comment
gevang
why not
arrayfun(@num2str, A, 'UniformOutput', false)? Same concept, same output, but you avoid the "from-cell" and "to-cell" conversions.In MATLAB, ['12' '25' '34' '466' '55'] is the same as a single string containing those numbers. That is to say:
['12' '25' '34' '466' '55']
ans =
12253446655
I need more context here for what you are trying to accomplish, but assuming you want to still be able to access each individual number as a string, a cell array is probably the best approach you can take:
A = [1 2 3]
num2cell(num2str(A))
(Of course, you'd still have to remove the stray spaces from the ans)
1 Comment
Doresoom
Even if you remove the stray spaces, you end up with a cell array of strings that contains each digit separately. Swap the order of
num2cell and num2str instead. cellfun(@num2str,num2cell(s),'UniformOutput',false) gets the job done nicely.