MATLAB: How to ensure that all the numbers use the same number of characters when converting from unicode to the native format in MATLAB 7.9 (R2009b)

MATLAB

I am converting an array of numbers from unicode to the native format using:
x = [4 87 44 9987];
u = unicode2native(num2str(x));
The length of the resulting array, 'u', depends on the input numbers. I would like to specify a fixed number of characters for all the elements of the array.

Best Answer

In order to specify a fixed number of characters to be used for all characters in the array, use the SPRINTF function with a syntax similar to:
x = [4 87 44 9987];
str = sprintf('% 8d',x)
u = unicode2native(str)
In this example, each element of the resulting string will contain 8 characters, padded with spaces (ASCII character 32).