Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display lines of text/string in a subplot in MATLAB?

I have a set of subplots in a figure in MATLAB. I produce a set of tests which are connected to the graphs in these subplots. I would like to have a separate subplot which presents these strings. I do not want to annotate a graph or produce a graph which prints text onto it. I want to print around 5 lines of text onto a subplot.

I tried manually placing the strings at coordinates but it got messy. Is there a straightforward approach?

like image 536
Vass Avatar asked Feb 17 '23 02:02

Vass


1 Answers

Use the text() function, with the 'Parent' property set to the handle of your subplot, i.e.,

figure;
h1 = subplot(2, 1, 1);
% Do some plotting on h1

h2 = subplot(2, 1, 2);
text(0, 0, sprintf('%s\n%s\n%s ... etc', line1Str, line2Str, line3Str), 'Parent', h2);

Note that here I have passed the return from a sprintf in order to include newline characters in the string (text accepts new lines and displays them without problem).

You may also wish to centre the text in the axes, for which you would replace the first two arguments with xPos, yPos, where these are:

xl = xlim(h2); 
xPos = xl(1) + diff(xl) / 2; 
yl = ylim(h2); 
yPos = yl(1) + diff(yl) / 2; 
t = text(xPos, yPos, sprintf('%s\n%s\n%s ... etc', line1Str, line2Str, line3Str), 'Parent', h2);
set(t, 'HorizontalAlignment', 'center');

The last line sets the alignment to center as well.

like image 187
wakjah Avatar answered Feb 19 '23 15:02

wakjah