Convert str to HTML format strings for use in uimenus and uicontrols
Updated 28 May 2014

View License

str2html Convert input str to html format for use in uicontrols/menus

uimenus and some uicontrols accept html format strings, e.g. pushbutton, listbox, popupmenu, menus, tooltips.

This function allows you to easily create html strings to display in the controls.

The function works by passing in a str and arg pairs:

htmlStr = str2html ( yourChar, P-V pairs )

yourChar 'a valid char string';

Valid arg pairs are:

'bold' true | ( false ) % bold format
'italic' true | ( false ) % italic format
'underline' true | ( false ) % underline format
'fontsize' 8 % size in int
'fontsize' '+2' % increment size from default
'colour' 'black' % colour of text
'fontname' 'MS Sans Serif' % font name
'superscript' '' % any superscript text ^
'subscript' '' % any subscript text ^
'bgcolour' 'white' % background colour
'prefix' '' % add your own prefix hmtl*
'suffix' '' % add your own suffix hmtl*
'userWarn' true | false % flag to display warnings

^ If sub & super script are added - then the subscript is added first.

* The idea of prefix and suffix is to add your own start and end tags to the html code that is generated - if only one is added a warning will be shown unless you add 'userWarn', false

For combing HTML format strings into one larger string, e.g. multi format on one line or multiple lines use:

yourCell{1} = str2html ( 'line1', pararm, value.... );
yourCell{2} = str2html ( 'line2', pararm, value.... );
yourCell{N} = str2html ( 'line2', pararm, value.... );

htmlStr = str2html ( yourCell, joinFormat )
The join format is a string to place between the cells, e.g. <br> or ' '.

The code uses persistent variables so you can change the default use following syntax. Note : No output.

str2html ( '*default*', 'bold', true )
str2html ( '*default*', 'fontname', 'Courier New' );

Special case for colour codes. e.g change default orange too:
str2html ( '*default*', colour:orange', '#FFBF40' );

This will change the defaults for THIS MATLAB session ONLY.

Reset all the changes made by the user.
str2html ( '*reset*' )


str = str2html ( 'YOUR TEXT', 'bold', 1, 'italic', 1, 'underline', 1, 'fontsize', 5, 'colour', 'red', 'fontname', 'Courier New' )

str = str2html ( 'Change Background Colour', 'bgcolor', 'red', 'superscript', 'SuperScript', 'subscript', 'SUB', ...
'fontsize', '+2' )

% This adds a bullet point to your string.
str = str2html ( 'Add your own HTML code:', prefix', '<li>', 'suffix', '</li>' );

For multi format lines or multiple lines use multiple calls:
line1{1} = str2html ( 'line #1', 'bold', 1, 'fontname', 'Courier New' );
line1{2} = str2html ( 'No format' );
line1{3} = str2html ( 'underline', 'underline', 1 );
line1{4} = str2html ( 'italic', 'italic', 1, 'subscript', 'mySub' );
line1{5} = str2html ( 'finish', 'colour', 'green', 'superscript', 'mySuper!!' );
str{1} = str2html ( line1, ' ' )
str{2} = str2html ( 'line #2', 'italic', 1, 'color', 'red' );
str2 = str2html ( str, '\n' );
h = uicontrol ( 'style', 'listbox' );
set(h,'tooltipString', str2 );

DEVELOPER NOTE: This file uses mlock

Submission includes a test script to how some examples.

Copyright Robert Cumming, rcumming @

Cite As

Robert Cumming (2024). str2html (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2013b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on MATLAB Report Generator in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes