Customizing Matlab labels

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>&#8704;&#946; <b>bold</b> <i><font color="red">label</html>';
jLabel = javaObjectEDT('javax.swing.JLabel',labelStr);
[hcomponent,hcontainer] = javacomponent(jLabel,[100,100,40,20],gcf);

Math symbols and HTML support in a Java JLabel

Math symbols and HTML support in a Java JLabel

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.

Related posts:

  1. Images in Matlab uicontrols & labels Images can be added to Matlab controls and labels in a variety of manners, documented and undocumented. ...
  2. Syntax highlighted labels & panels Syntax-highlighted labels and edit-boxes can easily be displayed in Matlab GUI - this article explains how. ...
  3. Setting axes tick labels format Matlab plot axes ticks can be customized in a way that will automatically update whenever the tick values change. ...
  4. FindJObj – find a Matlab component’s underlying Java object The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....
  5. Customizing editboxes Matlab's editbox can be customized in many useful manners...
  6. Rich Matlab editbox contents The Matlab editbox uicontrol does not handle HTML contents as do other uicontrols. In this article I show how this limitation can be removed....

Categories: GUI, Handle graphics, Hidden property, Java, Low risk of breaking in future versions, UI controls

Tags: , , , , , ,

Bookmark and SharePrint Print

4 Responses to Customizing Matlab labels

  1. Nick says:

    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)
    setBackground(jLabel, mycolor);

    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.

    • @Nick – JLabel’s Background and Foreground properties, and their corresponding getXYZ(),setXYZ() accessor methods, are common to all Java Swing components, together with several other basic properties (and their accessor methods).

      In this particular case, java.awt.Color can be constructed in several ways:

      1) Color(int) – the single integer RGB value you have reported in your comment

      2) Color(float,float,float) – accepts three values between 0.0-1.0 (R,G,B)

      3) Color(float,float,float,float) – as #2 above, with an additional 0.0-1.0 value for transparency (alpha)

      4) Color(int, int, int) – as #2 above but the RGB values are specified as integer values between 0-255. There is also a fourth value for alpha, as in #3 above

      5) Color.red etc. – there are several predefined static values: black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, and yellow

  2. Jerry says:

    I have little or no expereience with Java, but lots with MATLAB in general. When generating analysis reports, it’s common for me to have a text box or annotation box on a plot in a figure window. I would like to insert a hyperlink in the text box (the link would lead to the the test plan responsible for generating the data in the plot). When the figure with the plot and annotation is generated, it is typically printed to a PDF so that those who might be reading the report only need adobe and not a license of MATLAB.

    So,
    1) how is a hyperlink inserted into an annotation box on a figure? …
    2) … such that when it is printed (or saved) to PDF, the link is still a valid hyperlink in the PDF?
    Thanks

    • @Jerry – You can link text items to Matlab callbacks in the normal manner, by setting the handle’s callbacks (e.g., ButtonDownFcn). However, this does not get propagated to PDF output.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

<pre lang="matlab">
a = magic(3);
sum(a)
</pre>