Scale image to fit text boxes around borders
- by nispio
I have the following plot in Matlab:
The image size may vary, and so may the length of the text boxes at the top and left. I dynamically determine the strings that go in these text boxes and then create them with:
[M,N] = size(img);
imagesc((1:N)-0.5,(1:M)-0.5, img > 0.5); axis image; grid on;
colormap([1 1 1; 0.5 0.5 0.5]);
set(gca,'XColor','k','YColor','k','TickDir','out')
set(gca,'XTick',1:N,'XTickLabel',cell(1,N))
set(gca,'YTick',1:N,'YTickLabel',cell(1,N))
for iter = 1:M
text(-0.5, iter-0.5, sprintf(strL, br{iter,:}), ...
'FontSize',16, ...
'HorizontalAlignment','right', ...
'VerticalAlignment','middle', ...
'Interpreter','latex' );
end
for iter = 1:N
text(iter-0.5, -0.5, {bc{:,iter}}, ...
'FontSize',16, ...
'HorizontalAlignment','center', ...
'VerticalAlignment','bottom', ...
'Interpreter','latex' );
end
where br and bc are cell arrays containing the appropriate numbers for the labels. The problem is that most of the time, the text gets clipped by the edges of the figure. I am using this as a workaround:
set(gca,'Position',[0.25 0.25 0.5 0.5]);
As you can see, I am simply adding a larger border around the plot so that there is more room for the text. While this scaling works for one zoom level, if I maximize my plot window I get way too much empty space, and if I shrink my plot window, I get clipping again. Is there a more intelligent way to add these labels to use the minimum amount of space while making sure that the text does not get clipped?