Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Customizing Matlab labels

November 11, 2009 12 Comments

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);

%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. Transparent labels – Matlab labels can be set to a transparent background as well as padding. ...
  2. Customizing axes part 5 – origin crossover and labels – The axes rulers (axles) can be made to cross-over at any x,y location within the chart. ...
  3. Customizing axes tick labels – Multiple customizations can be applied to tick labels. ...
  4. Images in Matlab uicontrols & labels – Images can be added to Matlab controls and labels in a variety of manners, documented and undocumented. ...
  5. Customizing Matlab uipanels – Matlab uipanel controls can be customized using Java in ways that are impossible with plain Matlab. ...
  6. Hyperlink text labels – Hyperlink labels can easily be added to Matlab GUIs. ...
FindJObj GUI Handle graphics Hidden property HTML Java uicontrol
Print Print
« Previous
Next »
12 Responses
  1. Nick May 28, 2010 at 19:00 Reply

    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.

    • Yair Altman May 29, 2010 at 11:12 Reply

      @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 March 8, 2013 at 10:16 Reply

    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

    • Yair Altman March 9, 2013 at 17:06 Reply

      @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.

  3. MB July 21, 2015 at 06:00 Reply

    How can you implement this feature into an uicontrol? I use the GUI Layout Toolbox and this is the code:

    labelStr = '&#8704;&#946; <b>bold</b> <i>label';
    jLabel = javaObjectEDT('javax.swing.JLabel',labelStr);
    [hcomponent,hcontainer] = javacomponent(jLabel,[100,100,150,20],gcf);
     
    handles.box{1} = uiextras.HButtonBox;
    handles.a = uicontrol('Parent', handles.box{1}, 'String', string);

    labelStr = '&#8704;&#946; <b>bold</b> <i>label'; jLabel = javaObjectEDT('javax.swing.JLabel',labelStr); [hcomponent,hcontainer] = javacomponent(jLabel,[100,100,150,20],gcf); handles.box{1} = uiextras.HButtonBox; handles.a = uicontrol('Parent', handles.box{1}, 'String', string);

    where string is supposed to be the text from [hcomponent,hcontainer]

    Thanks.

    • Yair Altman July 21, 2015 at 08:46 Reply

      @Miguel – simply set your handles.a button’s String property value to labelStr

  4. Maciek October 30, 2015 at 07:44 Reply

    When i put this code into my m-file and i run my GUI, the only inscription which I can see is “bold”, where I made a mistake ?

    • Alex November 9, 2015 at 07:06 Reply

      Because author forgot the closing </i> tag, obviously.

      • Yair Altman November 9, 2015 at 07:14

        @Alex – closing tags are not necessary in HTML labels used in Matlab (or Java Swing in general).

        The reason that @Maciek doesn’t see the red “label” word is because his component is simply too narrow. When the container is too narrow, HTML simply truncates the extra text. If you increase the component width from 40 pixels to 100 pixels, you will see the entire text nicely.

  5. Alex November 9, 2015 at 07:20 Reply

    My question is probably stupid, but where I should actually place the code, mentioned in article? The GUI.m file has several different functions and I’m not sure what all of them do (except callbacks), because of my small experience with matlab gui (started today).

    • Yair Altman November 9, 2015 at 07:23 Reply

      @Alex – this blog handles advanced topics that are quite often beyond the basics. You obviously need to learn the basics first. You can start here or here.

    • Alex November 9, 2015 at 07:37 Reply

      Okay, thanks for the advice, Yair.

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitable (6) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top