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

Matlab's HG2 mechanism

A few days ago I posted [1] a lengthy article about Matlab’s undocumented feature function. In it, I mentioned a feature called HG2, that I believe merits a dedicated article, due to its potential high impact on future Matlab releases.
HG2, which presumably stands for “Handle Graphics 2nd generation”, was already reported in the past as an undocumented hidden figure property [2] (UseHG2). In normal Matlab usage, this boolean property is ‘off’:

>> get(gcf,'usehg2')
ans =
off

HG2 is mentioned in quite a few Matlab files:

Obviously, much effort was invested in HG2 functionality. The fact that HG2 has been under development at least since 2007 (when I first discovered and reported [3] it) seems to indicate a major upheaval in Matlab’s Handle Graphics mechanism. This hunch is reinforced by cryptic comments made by MathWorks personnel over the past few years that they are indeed looking at the HG system, which in their opinion is nearing its limitations. Perhaps I’m mixing unrelated stuff here, but it does make sense in light of Matlab’s push of its OOP class system over the past few releases.
To preview this HG2 system, we need to turn it on. Unfortunately, when we set the figure’s UseHG2 to ‘on’ there doesn’t seem to be any visible effect. However, this changes after we use the corresponding ‘UseHG2’ feature using the feature function (this caused lots of nasty-looking errors in past releases but works ok in R2010a):

>> feature('usehg2',1)

The /ja/xlate file (which is used in conjunction with the undocumented xlate function to translates Matlab messages from English to Japanese) has another key to unlocking HG2: This file contains the following message: “feature(‘useGBT2’) is only available when Matlab is started with -hgVersion 2 option.“. So let’s do as the xlate message advises and start a new Matlab session with the undocumented “-hgVersion 2” command-line option. Now feature(‘usehg2’) is true by default and we can test the HG2 system.
Matlab looks basically the same in HG2 as in HG1. All the regular graphic functions behave just as we would expect from the existing (HG1) implementation. There are two major differences though:

The latest Matlab releases have shown how the Matlab handle class can be extended using user-created derived classes. It stands to reason that so do all the new HG2 objects. This would theoretically enable Matlab programmers to customize graphic objects appearance to suit their needs in a more intuitive manner than possible using HG1.
Many mysteries remain:

HG2 is still buggy, which explains why it is still not officially released. For example, the inspect(gca) function crashes Matlab, figure toolbars/menubars are missing, and some properties that are available in HG1 are missing in HG2. Also, we can add Java components to a Matlab figure using javacomponent as in HG1 (the returned container handles is a ui.HGJavaComponent class handle), but we get an error when we close the figure…
Still, with all this effort invested into HG2 I believe that it is only a matter of time before HG2 becomes officially released. This could happen perhaps even as soon as the upcoming R2010b release, but with the current state as seen above I suspect it will not happen before 2011. Also, my gut feeling is that Matlab will define any release that includes HG2 as a major release and we will finally have Matlab 8.0.
I would dearly love to hear any further information anyone discovers about HG2 and related issues. Please share your findings by email or in the comments section below.
Addendum Oct 3 2014: HG2 is finally released in Matlab release R2014b (8.4). It took more than 4 years after this article was posted for Mathworks to sort out all the problems in the new graphics engine. While HG2 is now fully supported, it contains numerous undocumented features and properties. I plan to discuss them in a separate upcoming series of articles.

13 Comments (Open | Close)

13 Comments To "Matlab's HG2 mechanism"

#1 Comment By Drazick On May 7, 2010 @ 05:07

That would be great if Mathworks would rewrite Matlab’s graphic stack.
It needs a major performance overhaul.

#2 Comment By SrSwEngr On August 10, 2010 @ 08:21

MATLAB should say goodbye to JAVA for all kinds of reason, performance is just one of them.

#3 Pingback By Is it possible to extend graphics objects’ functionality through inheritance? – Programmers Goodies On September 7, 2011 @ 08:01

[…] is a suggestion that it may be possible to extend the new HG2 classes here but this is not official Mathworks information […]

#4 Pingback By HG2 update | Undocumented Matlab On May 15, 2013 @ 17:38

[…] Exactly three years ago, I posted information (here and here) about Matlab’s upcoming new graphics engine, so-called HG2 […]

#5 Comment By Chandrakanth On May 16, 2013 @ 21:15

Any idea how to enable HG2 on linux/osx versions? Even after setting feature to use hg2, it returns hg1 handles in 8.0
Thanks.

#6 Comment By Yair Altman On May 17, 2013 @ 00:11

@Chandrakanth – you need to use the -hgVersion 2 startup switch in your Matlab command-line. feature is not the way

#7 Comment By ZZ On November 7, 2013 @ 05:04

This undocumented feature seems really attractive. I tried “open /Applications/MATLAB_R2013a.app -hgVersion 2” in the terminal in the OSX system, but it does not run the app. Would you teach me a right command ?

many thanks!

#8 Comment By Malcolm Lidierth On November 7, 2013 @ 09:25

@ZZ
I am not near a Mac at the moment, but think you need to add /bin/matlab.exe to the end of the application path.
ML

#9 Comment By zz On November 7, 2013 @ 11:16

Thank you for your reply. Now it works quite well by following Chandrakanth’s method.

#10 Comment By Chandrakanth On November 7, 2013 @ 10:25

@ZZ
add /bin/matlab (NO exe)
/Applications/MATLAB_R2012b.app/bin/matlab -hgVersion 2
Works for me.

#11 Comment By zz On November 7, 2013 @ 11:20

A bunch of thanks !!!!
In fact I’ve tried “open /Applications/MATLAB_R2012b.app/bin/matlab -hgVersion 2” rather than your line. Now it works and I should to explore it more.

#12 Pingback By Customizing axes rulers | Undocumented Matlab On October 8, 2014 @ 07:43

[…] feed for updates on Undocumented Matlab topics.Over 4 years have passed since I’ve posted my scoop on Matlab upcoming new graphics system (a.k.a. HG2, Handle Graphics version 2). At that time HG2 […]

#13 Pingback By Render MATLAB figure in memory – DexPage On July 25, 2015 @ 14:59

[…] function named CAPTURESCREEN (although it seems that it will be using PRINT for the upcoming HG2 system where there is a new -RGBImage print […]