Reputation: 4721
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?
Upvotes: 4
Views: 1068
Reputation: 125854
You could use the textwrap
function in one of two ways:
Wrap the text to fit within a text uicontrol:
hText = uicontrol('Style', 'Text', 'Position', ...(some starting position)... );
[wrappedText, newPosition] = textwrap(hText, {description});
set(hText, 'String', wrappedText, 'Position', newPosition);
Wrap the text at a fixed number of columns before plotting with text
:
wrappedText = textwrap({description}, 20);
text(0.5, 0.5, wrappedText, 'FontSize', 14', 'FontWeight', 'Bold', ...
'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');
Upvotes: 2
Reputation:
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.
Upvotes: 3