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

GUI formatting using HTML

Posted By Yair Altman On April 5, 2017 | 6 Comments

As I’ve mentioned several times in the past, HTML can be used for simple formatting of GUI controls [1], 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 [2], 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 [3]) 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   = 'Left-aligned';  % left-align within a button
hTable.Data{2,1} = '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 [4] 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   = 'Yellow';  % bgcolor within a button
hTable.Data{2,1} = '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} = '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 [5].

[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 [5]), 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 Eric.Sargent 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 [6] 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 Eric.Sargent 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 [7] 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!

Categories: Medium risk of breaking in future versions, UI controls, Undocumented feature

6 Comments (Open | Close)

6 Comments To "GUI formatting using HTML"

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

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.

#2 Comment By Fareed On June 1, 2018 @ 16:31

Hi Yair,

Is this the only way to display image inside uitable cell? I need to pass an output from imcrop into it. Do i really need to save it as an image file first then call it by the html image linking?

#3 Comment By Yair Altman On June 1, 2018 @ 16:46

@Fareed – no, it is also possible to use a Java table cell renderer, but this requires knowledge of Java and a deeper use of undocumented aspects (i.e. accessing the uitable’s underlying JTable component, and then setting its column cell renderer accordingly). The HTML method that I described is much simpler to implement, so it is more suitable to the majority of users, but if you know your way around Java then a table cell renderer might well be useful for you.

#4 Comment By fareed On June 1, 2018 @ 17:28

I could not display the image in appdesigner uitable. It is a known for this method?

Sorry to ask you this because i have been learning to design gui using appdesigner and testing it ability to run task that i can think of. I am not sure whether the appdesigner is relatively new so it has its limit or i need to start with GUIDE first to fully utilise matlab gui design.

%% This work by showing tables inside a new figure.
pic = fullfile(matlabroot, 'toolbox', 'images', 'icons', 'icon_info.png');
uitable('Data', {['']})

% Does not work for uitable in app designer
app.UITable2.Data = ({['']});

#5 Comment By Yair Altman On June 1, 2018 @ 17:40

AppDesigner creates figures that do not [currently] support HTML or Java customizations. To use those, you need to use legacy figures using GUIDE or the figure/uicontrol/uitable functions.

#6 Comment By fareed On June 1, 2018 @ 17:55

Thank you for your reply. You are awesome , have a nice day sir.

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

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

URLs in this post:

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

[2] use of Java: http://undocumentedmatlab.com/blog/button-customization

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

[4] another post: http://undocumentedmatlab.com/blog/aligning-uicontrol-contents

[5] in this post: http://undocumentedmatlab.com/blog/uitable-cell-colors

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

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

[8] Sending HTML emails from Matlab : https://undocumentedmatlab.com/articles/sending-html-emails-from-matlab

[9] HTML support in Matlab uicomponents : https://undocumentedmatlab.com/articles/html-support-in-matlab-uicomponents

[10] GUI integrated HTML panel : https://undocumentedmatlab.com/articles/gui-integrated-html-panel

[11] Formatting numbers : https://undocumentedmatlab.com/articles/formatting-numbers

[12] Aligning uicontrol contents : https://undocumentedmatlab.com/articles/aligning-uicontrol-contents

[13] Customizing uifigures part 3 : https://undocumentedmatlab.com/articles/customizing-uifigures-part-3

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