How to update title of plot without it "wiggling"
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(4 个)
Mathieu NOE
2021-9-10
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
0 个评论
Image Analyst
2021-9-10
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
0 个评论
DGM
2021-9-10
编辑:DGM
2021-9-10
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.
0 个评论
Walter Roberson
2021-9-10
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!