MATLAB: Why is a blank ignored in strcat

MATLABstrcat blank

as an example, the following code:
strcat(num2str(1,'%02.0f'), '-', num2str(2), ' ', num2str(3), ':', num2str(4,'% 2.0f'))
produces:
01-23:4
whereas I want:
01-2 3: 4
Seems pretty simple but … would someone help please?

Best Answer

'% 2.0f' adds a leading space on demand only, so you want: ' %2.0f'.
But this would be nicer, faster and less confusing:
sprintf('%02.0f-%d %d: % 2.0f', 1, 2, 3, 4)
To get around this ugly space gimmicks, which come from the backward compatibility to times before cell strings have been invented, I'm using a dedicated function to join strings: FEX: CStrCatStr. But this is designed for cell strings and for strings horzcat works sufficiently without deleting spaces smartly.
Related Question