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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With