5

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.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
Mohammad
  • 145
  • 8
  • 1
    Did you try it with `annotation`? – Dev-iL Dec 20 '18 at 17:38
  • @Dev-iL No, How? – Mohammad Dec 20 '18 at 17:39
  • 1
    You are placing your `text` object in a 3D domain, so depending on your viewpoint there is always a case where it will be _behind_ the graph. Just find the coordinate set `(x,y,z)` which place your text in front of the graph _in the final view_ – Hoki Dec 20 '18 at 17:40
  • @Hoki What do you mean? – Mohammad Dec 20 '18 at 17:40
  • I mean the `text` object is placed in the same 3D domain as the data you plot, so the relative position of your patch and your text matter. It might be better to use [`annotation`](https://mathworks.com/help/matlab/ref/annotation.html) object as @Dev-iL suggested because they will always be on top of the `axes`, so on top of anything plotted in the figure. – Hoki Dec 20 '18 at 17:43
  • Yes, but the problem with `annotation` is that it doesn't accept rotation argument. – Mohammad Dec 20 '18 at 17:46
  • How can I rotate the text generated by `annotation`? – Mohammad Dec 20 '18 at 17:48
  • @Dev-iL How can I rotate my text? – Mohammad Dec 20 '18 at 17:56
  • @Mohammad Use an `annotation` of type [`TextArrow`](https://www.mathworks.com/help/matlab/ref/matlab.graphics.shape.textarrow-properties.html), which has a `TextRotation` property (instead of `TextBox`, which doesn't) and make the arrow invisible (set (`LineStyle` and `HeadStyle`) and/or `Color` to `none`). – Dev-iL Dec 20 '18 at 18:00
  • @Dev-iL Oh great! Thanks. – Mohammad Dec 20 '18 at 18:02

1 Answers1

7

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

Dev-iL
  • 23,742
  • 7
  • 57
  • 99