How to update title of plot without it "wiggling"

2 vues (au cours des 30 derniers jours)
Frederik Rentzsch
Frederik Rentzsch le 10 Sep 2021
Modifié(e) : DGM le 10 Sep 2021
The following code resizes the title and therefore makes it wiggle, when changing from "0.00" to "10.00".
plot(rand(1,10),rand(1,10))
t = repmat([0 10],1,5);
for k = 1:length(t)
title(strcat("Time: ", pad(num2str(t(k),'%5.2f'),5,'left'), " s"));
pause(.5)
drawnow
end
I know that you can use the flag "0" in
num2str(t(k),'%05.2f')
to prepend the string with a zero. Then "the wiggle" is gone but I would like to don't have this leading zero. How can this be done?

Réponses (4)

Mathieu NOE
Mathieu NOE le 10 Sep 2021
hello
my 2 cents suggestion :
plot(rand(1,10),rand(1,10))
t = repmat([0 10],1,5);
for k = 1:length(t)
above10= t(k) - rem(t(k),10);
if above10>1
title(strcat("Time: ", pad(num2str(t(k),'%5.2f'),5,'left'), " s"));
else
title(strcat("Time: ", pad([' ',num2str(t(k),'%5.2f')],5,'left'), " s"));
end
pause(.5)
drawnow
end

Image Analyst
Image Analyst le 10 Sep 2021
The problem is that you're using the default font which is proportionally spaced. You need to switch to a monospaced font:
plot(rand(1,10),rand(1,10))
t = repmat([0 10],1,5);
for k = 1:length(t)
caption = sprintf('Time: %5.2f', t(k));
title(caption, 'HorizontalAlignment', 'center', 'FontName', 'courier');
pause(.5)
drawnow
end

DGM
DGM le 10 Sep 2021
Modifié(e) : DGM le 10 Sep 2021
The issue isn't with getting the right number of characters. It's an issue of character spacing.
plot(rand(1,10),rand(1,10))
t = repmat([0 10],1,5);
for k = 1:length(t)
%title(strcat("Time: ", pad(num2str(t(k),'%5.2f'),5,'left'), " s"));
h = title(sprintf('Time: %5.2f s',t(k))); % title is 13 chars in both cases
h.FontName = 'Courier'; % this is a terrible font choice, but it demonstrates the issue
pause(.5)
drawnow
end
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:
plot(rand(1,10),rand(1,10))
t = repmat([0 10],1,5);
for k = 1:length(t)
h1 = text(0,0,'Time: ','fontweight','bold','fontsize',11);
set(h1,'horizontalalignment','left','verticalalignment','bottom', ...
'units','normalized','position',[0.3579 1.004 0]);
h2 = text(0,0,sprintf('%5.2f s',t(k)),'fontweight','bold','fontsize',11);
set(h2,'horizontalalignment','right','verticalalignment','bottom', ...
'units','normalized','position',[0.6430 1.004 0]);
drawnow
if k ~= numel(t)
pause(.5)
delete([h1 h2]) % get rid of old text objects before redrawing them
end
end
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.

Walter Roberson
Walter Roberson le 10 Sep 2021
Do not use strcat() in exactly that way. When you use strcat() with character vectors as complete arguments, then strcat() eats leading and training space. It does not do that for character vectors that are part of a cell array.
Your code can be written a bit more easily as
title("Time: " + pad(num2str(t(k),'%5.2f'),5,'left') + " s")
This helps... but does not fix the problem.
The movement reduces if you use FontName 'fixed' and 'Interpreter', 'none'
But even with fixed font, it is rendering the double space before the decimal as less than two adjacent spaces.
In theory if you use the latex interpreter, you can specify spacing fairly closely; see https://tex.stackexchange.com/questions/74353/what-commands-are-there-for-horizontal-spacing . Not all of those will work.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by