Why does num2str not work for double digits

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?

 Accepted 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

5 Comments

Well that works thank you. But for me the most confusing think in Matlab are these % formatting operator. Without the % the code doesn't work. So what does it do?
As I see it, f stands for floating. d works too but I think you took f to be on the save side. But what does the 3.0 do? Is this the field width?
My pleasure.
Correct. That’s the field width. I set it to 3 (with no digits after the decimal) to allow for larger numbers or negative two-digit numbers. (In describing a format descriptor for numbers, it is necessary to allow for the sign, decimal point, and all the characters in the exponential notation if you use it. So the format descriptor to print the largest or smallest decimal numbers MATLAB can represent is '%23.15E'.)
I considered using a cell array (that I mentioned originally), but decided to stay with the array you used since I don’t know what your intended application is.
Interestingly, the num2str function allows for a format descriptor, but doesn’t create the correct field width as sprintf does.
Ok. But what if my num is now
num = [0 pi 19 exp(1) 20000];
%6.0f gives me only integers. I tried %6.3f or something similar that doesn't work. What % do I have to take? Maybe you could tell me a good site for information on the formatting operator.
I need this because I want to do a multicolumn Legend:
legend_text(i,:) = sprintf('Angle %3.0f',Angle_Vector(i));
legend(legend_text(:,:));
with Angle_Vector(i) equals the num(i)
You need to add a few esoteric tweaks (such as a regexp call) to make that work:
Angle_Vector = [0 pi 19 exp(1) 20000];
legend_text = regexp(sprintf('Angle %6.3f\n',Angle_Vector), '\n', 'split');
figure(1)
plot(1:10, randi(9, 5, 10))
legend(legend_text(1:end-1))
Stephen23
Stephen23 on 24 Mar 2016
Edited: Stephen23 on 24 Mar 2016
"a good site for information on the formatting operator."
Why not start by reading the very helpful written MATLAB documentation:
Everything about the format string is explained there, with examples

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 24 Mar 2016
Edited: Guillaume on 24 Mar 2016
Your original problem is that you're trying to stuff strings of different lengths into a matrix.
On the first round of the loop, X does not exist. So the line,
X(i, :) = ...
instructs matlab to create a X matrix with 1 row (i = 1) and as many columns as necessary to fit the right hand side, that is 10 columns when num(i) is a single digit.
On subsequent rounds of the loop, X already exists so the line
X(i, :) = ...
tells matlab to fill row i and column 1:10 with the right hand side. However, if num(i) is two digits, then the right hand side ends up being 11 characters. At this point matlab tells you it can't put 11 characters in 10 columns.
Using sprintf with a field with forces all the strings to be the same length (as long as no number has more digits than the field width.
In general it's not a good idea to use a matrix to store multiple strings unless they're more or less the same lengths. All rows of a matrix have to have the same length, so shorter strings must be padded with spaces. Typically you store multiple strings in cell arrays, where each cell can be a different length. In your case:
num = [0, 10, pi, Inf];
X = cell(1, numel(num));
for i = 1 : numel(num)
X{i} = ['Hello Nr.',num2str(num(i))]; %I personally prefer sprintf to num2str
end
celldisp(X)
Note that it's then trivial to transform the cell array into a matrix of character with char. Matlab automatically pads the shorter strings:
char(X)
but to be honest there's no advantage to storing strings in matrices.

2 Comments

Thank you for the answer. It seems sprintf is the better option. But to get it working I need these % formatting operator. For me the most confusing think in Matlab are these formatting operator.
Note that matlab inherits these % formatting operator from C. It's well worth learning them. Granted they may look complex at first but they're also incredibly powerful.
You should be able to find plenty of tutorials on the web about sprintf (in matlab, or C, the format string is more or less the same)

Sign in to comment.

Categories

Asked:

on 24 Mar 2016

Edited:

on 24 Mar 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!