MATLAB: How to rotate a textbox annotation in MATLAB 7.9 (R2009b)

annotation;MATLABrotatetext;vertical

I am looking for a way to create a textbox annotation in a figure in a vertical orientation. I know that it is possible to rotate a TEXT object, however it is not possible to rotate an annotation textbox. I need to fix the text box position with respect to the figure coordinates, therefore, I need to use textbox annotation objects, as the TEXT objects coordinates would change with the axes limits.

Best Answer

The ability to rotate annotation textboxes is not available in MATLAB 7.9 (R2009b). It is possible to rotate the child text object of the annotation box but not the entire annotation box.
As a workaround, you can either use a text object or a textarrow annotation in place of the textbox annotation, as shown in the following examples:
plot(1:10)
h=annotation('textarrow',[0.5 0.5],[0.5 0.5],'string','textarrow annotation', ...
'HeadStyle','none','LineStyle', 'none', 'TextRotation',90);
%%now change axes limits - note that the annotation maintains its position
set(gca,'Ylim',[0 6],'Xlim',[0 8]);
As an alternative, when creating a text object using the TEXT function, recalculate the position of the object when changing the axes limits:
figure
plot(1:10)
tx=6;
ty=5;
text(tx,ty,'text textbox','Rotation',90);
%second figure for comparison
figure
plot(1:10)
h=text(tx,ty,'text textbox','Rotation',90);
%change axes limits and reposition text object accordingly
xl=get(gca,'Xlim');
yl=get(gca,'Ylim');
newxl=[0 6];
newyl=[0 8];
scalex = diff(newxl)/diff(xl);
scaley = diff(newyl)/diff(yl);
pos = get(h, 'Position');
tx = pos(1);
ty = pos(2);
offsetx = tx-xl(1);
offsety = ty-yl(1);
set(gca,'Xlim',newxl,'Ylim',newyl);
set(h, 'Position', [newxl(1)+offsetx*scalex newyl(1)+offsety*scaley]);