The issue isn't with getting the right number of characters. It's an issue of character spacing.
h = title(sprintf('Time: %5.2f s',t(k)));
The default font is a variable-width font, and so the title geometry depends on which particular characters are in the string, even if the number of characters never changes. Using a monospace font fixes the issue, but it might be a problem if you like the font you're using.
In that case, i'm not sure what the next-best solution would be. I imagine a person could concoct a cumbersome attempt to compensate for the variable width by manually padding text with spaces of various widths:
Or one could build the title out of multiple juxtaposed text objects, right-justifying the object containing the number and locating off of its right edge so that its apparent location doesn't change as its geometry varies.
EDIT: something like this:
h1 = text(0,0,'Time: ','fontweight','bold','fontsize',11);
'units','normalized','position',[0.3579 1.004 0]);
h2 = text(0,0,sprintf('%5.2f s',t(k)),'fontweight','bold','fontsize',11);
'units','normalized','position',[0.6430 1.004 0]);
You can figure out about what the position values should be by looking at the Extent property of the title object you're trying to replicate. 0.3579 is approximately the same as h.Extent(1), and 0.6430 is approximately h.Extent(1) + h.Extent(3) -- the left and right edges of the bounding box, respectively.