How to fix/steady the position of a counter in plot title.

2 views (last 30 days)
I have a script for plotting animated surf and trisurf plots. It has a time counter in the title fed by the 'for' loop index. With each redraw, the counter moves position depending on the number of numerals in the number, so it flickers quite annoyingly in the 30 fps resultant video that the script writes.
I've tried specifying the title position with each redraw, specifying its justification and also creating a data-free plot first, holding it and having the animated plot over the top (but I couldn't get that to work). Haven't managed to find a similar problem on Answers.
Extremely grateful for any suggestons of things to try. Cheers.

Answers (2)

Bjorn Gustavsson
Bjorn Gustavsson on 25 Nov 2020
Perhaps you can use the same type of zero-padding often used for filenames? Something like:
tstr = sprintf('%04d',loop_idx);
Or however large indices you have to make space for. This might not be the prettiest, but perhaps you can use some of the other formating-flags, se the documentation for fprintf for posibilities.

dpb on 25 Nov 2020
Edited: dpb on 25 Nov 2020
Use a fixed-width format string to build the string to display...
title(num2str(t1(3)*1000,'Simulation time = %8.3f ms'))
t1 happened to be a variable in my workspace from an earlier Answer; obviously "salt to suit".
You may still see some movement unless you use a fixed-width font as otherwise the rendering of different numbers and number of blanks will result in a little bit of difference in the kerning/width of proportional fonts.

Community Treasure Hunt

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

Start Hunting!