How to write a subscript and superscript in Matlab that will be displayed in Graphical User Interface(GUI)?

Chris picture Chris · Nov 2, 2011 · Viewed 18k times · Source

I want to have a subscript and superscript in my GUI in Matlab. So far I have tried to use x_2 and x^2 and neither of them work. Is there anyone who knows how to do this?

Thanks in advance!

Answer

Smash picture Smash · Nov 2, 2011

From http://www.mathworks.com/matlabcentral/newsreader/view_thread/158410#398940

You can use an undocumented feature of all Matlab uicontrols, which is the fact that they use underlying Java Swing controls, and these in turn accept any valid HTML strings. So you can do the following for example:

uicontrol('string','<html><b>1<sub>2</sub>3<sup>4</sup>5</b></html>')

This is equivalent to the tex string '\bf1_23^45\rm'. You can set font faces, colors, sizes, bold/italic and any other valid HTML 3.0 property. It's limited, but should do the trick in most conceivable cases.

The same is true for tooltips, by the way: try setting multi-line (<br>) multi-colored (<font color="red"> ... </font>) tooltip once and you'll never use the standard boring single-line black tooltip again...

And just in case you were wondering - yes, it also works for menus, listboxes etc.