The text.Extent property reports inconsistent values

4 Ansichten (letzte 30 Tage)
Does anybody know why the Extent property of a TEXT object in a uiaxes reports the wrong value?
The code snippet below demonstrates the problem. It creates a text object which reports
textobj.Extent = [1 1 52.545 19]
but after calling redraw it has changed to
textobj.Extent = [1 1 65 23].
The redraw event has mysteriously changed the width and height of the Extent. Does anybody know why??
I am using Matlab R2019b.
% construct a new figur
fig = uifigure();
% construct a new axes
ax = uiaxes(fig, 'Units','pixels');
% add a text object to the axes
textobj = text(ax,1,1, ...
'x = \int y', ...
'Units','pixels', ...
'FontUnits','pixels', ...
'FontSize',16, ...
'VerticalAlignment','bottom');
% Here the Extent is reported as [1 1 52.5450 19.0000]
textobj.Extent
% Force a redraw of the graphics
drawnow;
% Here the Extent is reported as [1 1 65 23]
textobj.Extent

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 30 Jan. 2021
There are properties of an object that are not finalized until you render the object to the screen.
The first time you are probably getting an Extent that does not take into account that you are using Tex, so the Extent probably only reflects what would be the case if you were using plain characters. Then the updates get released, all pending changes get made, and the graphics system figures out what the real size of the text is.
It is not efficient for the graphics system to update all properties immediately in a setup in which properties can be changed by assigning them values. For example,
h = line([1 2], [3 4]);
h.XData = 1:20;
If all graphics changes were made immediately, then the graphics engine would have to complain right there about the inconsistencies in size between XData (newly set to a list of 20 values) and h.YData (still a list of length 2), even though the next line might be
h.YData = sin(pi/5*(1:20))
The actual situation at present is that the consistency is not checked until properties have to be "finalized" in order to send them to the graphics engine.

Weitere Antworten (0)

Kategorien

Mehr zu Scripts finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by