MATLAB: Does num2str not work for double digits

digitsdoublenum2str

Hey everybody,
why does
num = [0 1 2 3 5];
for(i=1:5)
X(i,:) = ['Hello Nr.',num2str(num(i))];
end
disp(X(:,:))
work fine but when I change num to have numbers with double digits like
num = [0 1 12 3 5];
it doestn't work anymore?

Best Answer

You can use a cell array, but probably the easiest solution is to use the sprintf function:
num = [0 1 12 3 5];
for(i=1:5)
X(i,:) = sprintf('Hello Nr.%3.0f',num(i));
end
disp(X(:,:))
Hello Nr. 0
Hello Nr. 1
Hello Nr. 12
Hello Nr. 3
Hello Nr. 5
Related Question