MATLAB Answers

Can you set a plot property as an equation?

3 views (last 30 days)
Michael on 16 Aug 2018
Commented: Michael on 16 Aug 2018
I would like to set up a property as an equation. This could apply to a figure, axis, etc. property, but I am specifically interested my making my annotation positions dynamic with variables, so that if I used commands like 'xlim' or 'axis equal' after I place the annotations, the annotation locations move appropriately. I used this guide to properly place my annotation on the figure, but when I use commands like 'xlim' or 'axis equal' later in my script, the annotations stay in the original positions in my figure window and don't track with where they should be like in the plot domain. I though maybe I could make the Position elements variable like this:
h_ann = annotation('textbox',[0.5 0.5 0.1 0.1 ],'String','my annotation');
a = 0.2;
b = 0.3;
but the 'set' command throws the error:
Error using handle.handle/set
Invalid or deleted object.
Is there a way to do this so that later in my program, if I update a or b, my annotation location would updated? I realize that I could directly change h_ann with set later in the program, but this would be messy.
Here is an example where I initially place an annotation...
then change some axis properties and the annotation is no longer where I want it...
The position in the figure window is maintained, but I'd like to link the position to axis properties.
Michael on 16 Aug 2018
I thought I needed to make the elements of the Position vector dynamic to accommodate future changes to the axes, but the answers provided using text.m take care of this for me.

Sign in to comment.

Accepted Answer

jonas on 16 Aug 2018
Edited: jonas on 16 Aug 2018
You can use the text function instead, where the position is linked to the axis.
Also, the syntax here is wrong
Should be one of the following:
set(h_ann, 'Position',a+b);
h_ann.Position = a+b
The position property should be a 1x4 vector, not a scalar

More Answers (1)

dpb on 16 Aug 2018
Edited: dpb on 16 Aug 2018
Syntax problems...
1. You're mixing "dot" notation with set -- use either
set(hAnn,'Position', [l b w h]) or
hAnn.Position=[l b w h];
where [l b w h] is the four-element position vector of [left bottom width height]
2. The position value must be the four-vector; you've tried to set only one. There's nothing wrong in using variables to build the four-vector but must pass the vector.
If you want to only modify the x-position, say, then retrieve current position and calculate the new--
posn=hAnn; % retrieve current
posn(1)=posn(1)+a; % move _by_ a, or
posn(1)=a; % move _to_ a
hAnn.Position=posn; % set new position
You can write a function to do something like this and have it tied to a callback function for example as well if wanted.
  1 Comment
Michael on 16 Aug 2018
Thanks! Sorry, I was a little sloppy on the syntax.

Sign in to comment.





Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!