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….

File Managing in MatLab Editor

Today rather by coincidence I’ve found a cool little feature of MatLab files editor, which makes file handling more comfortable (at least for me).

By the standard setup all opened files are shown like tabs at the bottom, see. picture below. If you have too many of them and it happens quite often if you work on a project, the tendency to lose overview is obvious. Longer file names will be cut and you have to scroll horizontally to get the one you want.


Like I mentioned, today I was really upset by scrolling and looking what files are selected, so I started clicking and discover that like in Windows you can determine where this bar has to be. Just right click on the bar and you see this little menu, got it?


For myself the best position is on the right side. This way I see the code and have full overview about all files opened right now. Additionally sometimes it’s useful to check the “Alphabetize” option. Of course if you have really many files opened,  you also have to scroll, but then you have a really large project to work on. What a pity 🙂

Try it out, it’s very simple, may be to obvious…