Can you set a plot property as an equation?
조회 수: 1 (최근 30일)
이전 댓글 표시
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;
set(h_ann.Position,a+b);
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.
댓글 수: 2
Geoff Hayes
2018년 8월 16일
편집: Geoff Hayes
2018년 8월 16일
Michael - if your original position is [0.5 0.5 0.1 0.1 ], then how should this be adjusted by the a and b? What are a and b?
Is there a way to do this so that later in my program, if I update a or b, my annotation location would updated?
Do you mean that if a or b change then the position of the annotation would automatically change?
채택된 답변
jonas
2018년 8월 16일
편집: jonas
2018년 8월 16일
You can use the text function instead, where the position is linked to the axis.
Also, the syntax here is wrong
set(h_ann.Position,a+b);
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
댓글 수: 0
추가 답변 (1개)
dpb
2018년 8월 16일
편집: dpb
2018년 8월 16일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!