Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap strings when plotting with `text`?

I have a long string that I would like to add to a subplot as descriptive text.

description = 'This kitchen has white cabinets and two blue chairs. The upper cabinet has a black microwave. The paper towels are above the trash can. There is a black garbage can just on the left of the blue chair. On its left there is a red fire distinguisher.';

I have tried to add new line characters after every sentence to make it fit better.

subplot(1,2,2);
with_new_lines = regexprep(description, '\.', '\.\n');
text( 0.5, 0.5, with_new_lines, 'FontSize', 14', 'FontWeight', 'Bold', ...
    'HorizontalAlignment', 'Center', 'VerticalAlignment', 'middle' ) ;

But it still does not fit properly within the axis.

Is there a way to wrap the string dynamically to fit the subplot?

enter image description here

like image 380
Cecilia Avatar asked Oct 16 '25 17:10

Cecilia


1 Answers

How about using an annotate box, with the FitBoxToText property off?

description = 'This kitchen has white cabinets and two blue chairs. The upper cabinet has a black microwave. The paper towels are above the trash can. There is a black garbage can just on the left of the blue chair. On its left there is a red fire distinguisher.';
figure;subH=subplot(1,2,2);
pos=get(subH,'Position');
annotation('textbox', pos,...
  'String', description,...
  'FitBoxToText','off');

You can change the location by changing the 1st two elements of pos ,which (I think) describe the left-bottom corner, but forget.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!