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

GUI formatting using HTML

Posted By Yair Altman On April 5, 2017 @ 11:26 pm In Medium risk of breaking in future versions,UI controls,Undocumented feature | 1 Comment

As I’ve mentioned several times in the past, HTML can be used for simple formatting of GUI controls [3], including font colors/sizes/faces/angles. With a bit of thought, HTML (and some CSS) can also be used for non-trivial formatting, that would otherwise require the use of Java [4], such as text alignment, background color, and using a combination of text and icons in the GUI control’s contents.


For example, a question that I am often asked (latest example [5]) is whether it is possible to left/center/right align the label within a Matlab button, listbox or table. While Matlab does not (yet) have properties that control alignment in uicontrols, we can indeed use HTML for this. There’s a catch though: if we simply tried to use <div align="left">…, it will not work. No error will be generated but we will not see any visible left-alignment. The reason is that internally, the text is contained within a snugly-fitting box. Aligning anything within a tight-fitting box obviously has no effect.

To solve the problem, we need to tell Matlab (or rather, the HTML interpreter used by the underlying Java control) to widen this internal box. One way to do this is to specify the width of the div tag, which can be enormous in order to span the entire available apace (<div width="999px" align="left">…). Another method is to simulate a simple HTML table that contains a single cell that holds the text, and then tell HTML the table cell’s width:

hButton.String   = '<html><tr><td width=9999 align=left>Left-aligned';  % left-align within a button
hTable.Data{2,1} = '<html><tr><td width=9999 align=right>And right';   % right-align within a specific uitable cell

centered (default) button label   right-aligned button label

Centered (default) and right-aligned button labels

Non-default alignment of uitable cells

Non-default alignment of uitable cells

I discussed the specific aspect of uicontrol content alignment in another post [6] last year.

Background color

The same problem (and solution) applies to background colors: if we don’t enlarge the snugly-fitting internal bounding-box, any HTML bgcolor that we specify would only be shown under the text (i.e., within the internal box’s confines). In order to display bgcolor across the entire control/cell width, we need to enlarge the internal box’s width (the align and bgcolor tags can of course be used together):

hButton.String   = '<html><tr><td width=9999 bgcolor=#ffff00>Yellow';  % bgcolor within a button
hTable.Data{2,1} = '<html><tr><td width=9999 bgcolor=#ffff00>Yellow';  % bgcolor within a specific uitable cell


We can also use simple CSS, which provides more formatting customizability than plain HTML:

hTable.Data{2,1} = '<html><tr><td width=9999 style="background-color:yellow">Yellow';

HTML/CSS formatting is a poor-man’s hack. It is very crude compared to the numerous customization options available via Java. However, it does provide a reasonable solution for many use-cases, without requiring any Java. I discussed the two approaches for uitable cell formatting in this post [7].

[Non-]support in uifigures

Important note: HTML formatting is NOT [yet] supported by the new web-based uifigures. While uifigures can indeed be hacked with HTML/CSS content (details [7]), this is not an easy task. Since it should be trivially easy for MathWorks to enable HTML content in the new web-based uifigures, I implore anyone who uses HTML in their Matlab GUI to let MathWorks know about it so that they could prioritize this R&D effort into an upcoming Matlab release. You can send an email to George.Caia at mathworks.com, who apparently handles such aspects in MathWorks’ R&D efforts to transition from Java-based GUIs to web-based ones. In my previous post I spotlit MathWorks user-feedback surveys [8] about users’ use of Java GUI aspects, aimed in order to migrate as many of the use-cases as possible onto the new web-based framework. HTML/CSS support is a natural by-product of the fact that Matlab’s non-web-based GUI is based on Java Swing components (that inherently support HTML/CSS). But unfortunately the MathWorks surveys are specific to the javacomponent function and the figure’s JavaFrame property. In other words, many users might be using undocumented Java aspects by simply using HTML content in their GUI, without ever realizing it or using javacomponent. So I think that in this case a simple email to George.Caia at mathworks.com to let him know how you’re using HTML would be more useful. Maybe one day MathWorks will be kind enough to post a similar survey specific to HTML support, or maybe one day they’s just add the missing HTML support, if only to be done with my endless nagging. :-)

p.s. – I am well aware that we can align and bgcolor buttons in AppDesigner. But we can’t do this with individual table/listbox cells, and in general we can’t use HTML within uifigures without extensive hacks. I merely used the simple examples of button and uitable cell formatting in today’s post to illustrate the issue. So please don’t get hung up on the specifics, but rather on the broader issue of HTML support in uifigures.

And in the meantime, for as long as non-web-based GUI is still supported in Matlab, keep on enjoying the benefits that HTML/CSS provides.

Automated bug-fix emails

In an unrelated matter, I wish to express my Kudos to the nameless MathWorkers behind the scenes who, bit by bit, improve Matlab and the user experience: Over the years I’ve posted a few times my frustrations [9] with the opaqueness of MathWorks’ bug-reporting mechanism. One of my complaints was that users who file bugs are not notified when a fix or workaround becomes available. That at least seems to have been fixed now. I just received a seemingly-automated email notifying me that one of the bugs that I reported a few years ago has been fixed. This is certainly a good step in the right direction, so thank you!

Happy Passover/Easter to all!

1 Comment (Open | Close)

1 Comment To "GUI formatting using HTML"

#1 Comment By OrO On April 6, 2017 @ 9:01 am

Your tips/solutions are always useful.
I regret that since all these years, Matlab still does not fully support HTML/CSS. It would help considerably the customization of GUI.

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

URL to article: https://undocumentedmatlab.com/blog/gui-formatting-using-html

URLs in this post:

[1] Image: http://undocumentedmatlab.com/feed/

[2] email feed: http://undocumentedmatlab.com/subscribe_email.html

[3] HTML can be used for simple formatting of GUI controls: https://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents

[4] use of Java: https://undocumentedmatlab.com/blog/button-customization

[5] latest example: https://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents#comment-403579

[6] another post: https://undocumentedmatlab.com/blog/aligning-uicontrol-contents

[7] in this post: https://undocumentedmatlab.com/blog/uitable-cell-colors

[8] user-feedback surveys: https://undocumentedmatlab.com/blog/mathworks-solicited-java-survey

[9] my frustrations: https://undocumentedmatlab.com/blog/couple-of-matlab-bugs-and-workarounds

[10] Icon images & text in Matlab uicontrols : https://undocumentedmatlab.com/blog/icon-images-in-matlab-uicontrols

[11] Spicing up Matlab uicontrol tooltips : https://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips

[12] Multi-line uitable column headers : https://undocumentedmatlab.com/blog/multi-line-uitable-column-headers

[13] Rich-contents log panel : https://undocumentedmatlab.com/blog/rich-contents-log-panel

[14] xlsread functionality change in R2012a : https://undocumentedmatlab.com/blog/xlsread-functionality-change-in-r2012a

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