As I was deliberating the topic of my weekly article, a new CSSM newsreader thread arrived today to immediately conclude the debate: The CSSM poster asked how Matlab labels can be modified to display non-ASCII characters such as the ∀ or β math symbols.
As you may recall, unlike axes text labels that support Tex/Latex, and unlike other uicontrols like buttons or listboxes that support HTML, text labels (uicontrol(‘Style’,’text’,…)) do not support text formatting or special symbols.
In the above-mentioned thread, Matt Whitaker, a longstanding CSSM contributor and a Matlab-Java veteran gave a solution that shows how seemingly difficult questions sometimes have simple solutions right beneath our noses. His solution was to simply replace the uicontrol label with a Java JLabel:
%show the 'for all' and 'beta' symbols labelStr = '<html>∀β <b>bold</b> <i><font color="red">label</html>'; jLabel = javaObjectEDT('javax.swing.JLabel',labelStr); [hcomponent,hcontainer] = javacomponent(jLabel,[100,100,40,20],gcf);
Note that the standard Matlab text uicontrol itself is very limited in the amount of customization it supports, even when accessing its underlying Java object using the FindJObj utility. This underlying Java object is a com.mathworks.hg.peer.utils.MultilineLabel extension of Swing’s bland javax.swing.JComponent. In fact, aside from some font and color customizations (also available via the Matlab HG properties), the most useful properties that are accessible only via the Java object are few. These include Border, HorizontalAlignment, VerticalAlignment and LineWrap. This is a very short list compared to the long list of corresponding undocumented properties in the other uicontrols.
I don’t know if this is obvious as I don’t know Java, but you can change the gray background of the JLabel by defining a java.awt.Color object and using setBackground:
mycolor = javaObjectEDT(‘java.awt.Color’, rgb_code)
The rgb_code is an integer that seems to be correspond to RGB as follows:
256^2*(0-255 red level) + 256*(0-255 green level) + (0-255 blue level)
So black = 0, and white is 256^2*255 + 256*255 + 255. You can cobble together highlighted sentences using this technique by aligning multiple JLabel objects in the figure.