Monday, March 08, 2010

Matlab: Convert numerical array to cell with string representation of numbers

Short Matlab script that takes an array (i.e. vector) of numbers and returns cell with fileds that are string representation of the numbers in the array.
function c = numarray2cellstring(a)
%function c=numarray2cellstring(a)
%INPUT
% a - vector of number ,e.g. [1 2 3 4.5];
%
%OUTPUT
% c - cell with string representation of the number is input array.
%EXAMPLE
% a=[1 2 4 6 -12];
% c=numarray2cellstring(a);
% c =
%
% '1' '2' '4' '6' '-12'
%
c={};
for i=1:length(a)
c{end+1}=num2str(a(i));
end
For example: >> a=[1 2 3.24 -43 4.56];
>> numarray2cellstring(a)

ans =

'1' '2' '3.24' '-43' '4.56'