Multiline Strings and Other Text Editing Features in MatLab GUI

Originally I intended to write only couple of lines about programmatically plot editing in MatLab. But then digging a bit more through the web, I found quite interesting stuff about possibilities you can do in MatLab with the GUI. So now it is a bit more, I hope you like it.

As I am a comfortable guy,  I find working in plot editor mode kind of annoying. I do not like clicking all around and enjoy much of doing things programmatically, especially things which have to be do 20-30 times or even much more often per day.

Today I had to plot several similar figures. All the figures needed a title, so instead doing this in editor mode, I wrote a line of code. Actually I wanted the title to be two lines long therefore multiline and thought a simple ‘\n’ like I know it from C++ would do it, but I was wrong.

The solution for multiline strings in MatLab is to handover the string as as a cell array where each entry? is a new line is . So if you want to give out

This is the first line

and this the second

just write

title( { ‘This is the first line’ ‘and this the second’} )

Simple need, simple solution.

Additionally while looking up on the web, I’ve found out another solution where actually the ‘\n’ – style input works. Just use ‘sprintf’ to format the text. So the above example would go like this:

title( ( sprintf(‘This is the first line \n this the second’ ) )

Of course you can also use value arguments as place holder:

test_string = ‘just testing’

title( sprintf( ‘print second line \n %s’, test_string ) )

where %s stands for a defined value like ‘just testing’ in this case.

So far, so good. But really interesting was to me to find out that you can use basic HTML commands to manipulate your figures beyond simple text strings. Actually it was new to me that you can define controls similar to those in PHP/HTML. By defining

handle = uicontrol

a simple button is created (default option for user interface control in MatLab). Of course be setting appropriate ‘Style’ you can create radio buttons, sliders, editable text fields and other useful controls, see uicontrol in the MatLab help.

Extened features come into play, when usage of ‘String’-property is made. Then you can just use HTML-tags and format the control in HTML Style, e.g.

uicontrol(‘Style’,’popup’, ‘Position’,[10,10,150,100], ‘String’, … {‘<HTML><BODY bgcolor="green">green background</BODY> </HTML>’, … ‘<HTML><FONT color="red" size="+2">Large red font</FONT></HTML>’,…'<HTML><BODY bgcolor="#FF00FF"> <PRE>fixed-width font’});

creates a nice and colorful pop-up menu. Having this you can improve your figure just by adding some simple HTML- tags.

Have fun playing around….

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s