matlabtextrotationmatlab-figurebringtofront

Bring text to the front of a plot


I have a figure in MATLAB. Then I add a text to it by typing,

b = text(0.5, 0.5, 'Detector action', 'Rotation', -70, 'FontSize', 25);

But the text goes behind the figure (See below), enter image description here

I also tried,

uistack(b, 'top');

but it didn't work.


Solution

  • The easiest way is not to bother with a text at all, but instead use an annotation, since such an object will be (at least by default) above the axes (and thus, anything plotted inside them).

    The trick with annotation objects, is that counter-intuitively, we need not use a TextBox, but instead a TextArrow, while making the arrow itself invisible.

    For example:

    figure(); membrane(); 
    annotation('TextArrow', [.5 .5], [.5 .5], 'String','Some text', 'TextRotation', -30, ...
               'HeadStyle','none','TextBackgroundColor','w' );
    

    enter image description here