- Undocumented Matlab - https://undocumentedmatlab.com -

Bug and workaround in timeseries plot

Posted By Yair Altman On September 7, 2011 | 4 Comments

I was about to write on something completely different today, when I discovered the following internal Matlab bug (and workaround) that I thought could be useful to some readers, so I changed my plans and the original article will see light next week (or the next, or whenever…).

The bug

Try the following code snippet:

today = fix(now);
ts = timeseries(rand(1,201), datestr(today-200:today));
hPanel = uipanel('units','norm','pos',[.2,.2,.5,.5]);
hAxes = axes('Parent',hPanel, 'units','norm');
plot(ts, 'Parent',hAxes);

As you can see, the plot’s x-tick labels are all bunched together:

Timeseries plot bug (note x tick labels)
Timeseries plot bug (note x tick labels)

The workaround

I debugged the problem and traced it to line #317 of …\R2011b\toolbox\matlab\timeseries\@timeseries\plot.m (in the R2011b installation):

axpos = hgconvertunits(f,get(ax,'Position'),get(ax,'Units'),'normalized',f);

This line uses hgconvertunits to get the figure-based normalized units of the axes. hgconvertunits is an internal semi-documented helper function that convert a Handle-Graphics position vector in one set of units into another position vector in other units based on some other reference HG object. For example, it can tell me what the position of an internal control is in normalized figure units, even if the control is deeply nested within multiple panels. Which is exactly what it tries to do in this case.
Unfortunately, hgconvertunits apparently has a bug that causes it to return the parent-based normalized position values rather than the figure-based ones in this particular case. This causes the function to think that the axes is much bigger than it really is, and therefore it uses more x-tick labels than it should.
This hgconvertunits bug may very possibly affect numerous other Matlab functions that use this built-in function. The correct fix is to fix the hgconvertunits function. However, since this is an non-modifiable internal function, I found the following workaround for the timeseries plot only, by replacing the above line with the following:

axpos = hgconvertunits(f,getpixelposition(ax,f),'pixel','normalized',f);

Timeseries plot fix (note x tick labels)
Timeseries plot fix (note x tick labels)

Now the x-tick labels appear nicely, including during resize and zooming.
This problem occurs on R2011b. I was able to recreate the same problem and use the same fix back to R2010b. I do not know how far earlier the bug goes…
I reported the problem today (1-FEM0L7 for anyone interested). If I learn of a patch for hgconvertunits I will post an addendum on this page.

Categories: Handle graphics, Low risk of breaking in future versions, Semi-documented function, Stock Matlab function

4 Comments (Open | Close)

4 Comments To "Bug and workaround in timeseries plot"

#1 Comment By jeff On December 22, 2011 @ 19:48


Ordered your book on Tuesday. Looking forward to reading it. Any word on a fix for the timeseries plot function?

#2 Comment By Yair Altman On December 23, 2011 @ 04:14

@Jeff – what do you mean by “any word on a fix?” – my entire post was about the fix… I don’t follow what you mean

#3 Pingback By Pinning annotations to graphs | Undocumented Matlab On December 12, 2012 @ 11:02

[…] use the undocumented hgconvertunits function to convert from pixel units into normalized figure units […]

#4 Comment By asmf On February 23, 2018 @ 13:24

In R2016b I replaced call to hgconvertunits with getpixelposition in dateTickPicker.

Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/bug-and-workaround-in-timeseries-plot

URLs in this post:

[1] Matlab compiler bug and workaround : https://undocumentedmatlab.com/articles/matlab-compiler-bug-and-workaround

[2] Plot performance : https://undocumentedmatlab.com/articles/plot-performance

[3] Bar plot customizations : https://undocumentedmatlab.com/articles/bar-plot-customizations

[4] Plot legend title : https://undocumentedmatlab.com/articles/plot-legend-title

[5] Plot LimInclude properties : https://undocumentedmatlab.com/articles/plot-liminclude-properties

[6] Undocumented scatter plot jitter : https://undocumentedmatlab.com/articles/undocumented-scatter-plot-jitter

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.