Lock position of text arrow on figure

7 次查看(过去 30 天)
From the figures attached, you can see the problem I'm having.
How would i code for the text arrow to have a locked position, where it would shrink and enlarge as the figure is zoomed out and in respectively. Or at least have the head be stuck at a fixed x&y position
Code I'm currently using, I tried the normal textarrow code, and attempted to fix the problem with the following:
%Red labelled rectangle highlighting the area of interest
rectangle ('position', [105 -7 1 1], 'edgecolor', 'r');
text(104.9,-12, 'Area of Interest','Color','red','FontSize',10,'FontWeight','bold');
% tail
[xt yt] = ds2nfu(105 ,-20);
% head
[xh yh] = ds2nfu(105.643, -6.653);
annotation('textarrow', [xt xh],[yt yh], 'String' , 'Panaitan Island');

回答(1 个)

Abhinaya Kennedy
Abhinaya Kennedy 2024-5-9
Hi Ishaan,
The code you provided uses data coordinates to specify the position of the text arrow. This means that the arrow will move and resize when you zoom in and out of the figure.
To fix this and have the text arrow maintain a locked position (head fixed) with the tail resizing based on the zoom level, you can use normalized units instead. Here's the modified code:
% Red labelled rectangle highlighting the area of interest
rectangle('position', [0.7 0.2 0.1 0.1], 'edgecolor', 'r');
text(0.69, 0.18, 'Area of Interest', 'Color', 'red', 'FontSize', 10, 'FontWeight', 'bold');
% Head position (fixed in normalized units)
xh = 0.8;
yh = 0.3;
% Tail position (adjustable based on data coordinates)
xt = 0.75;
yt = -0.1;
% Create text arrow with normalized units and adjustable tail
annotation('textarrow', [xt xh], [yt yh], 'String', 'Panaitan Island', 'Units', 'normalized');
Here,
  1. Normalized Units: The "Units", "normalized" argument is added to the "textarrow" annotation. This ensures the arrow position is specified relative to the figure window, keeping it fixed during zoom.
  2. Head position: The head coordinates ("xh" and "yh") are set as fixed values between 0 and 1 (normalized units).
  3. Tail position: The tail coordinates ("xt" and "yt") are defined using data coordinates. You can adjust these values based on your data to position the tail appropriately.
This approach will keep the text arrow's head fixed while the tail resizes based on the data coordinates and zoom level. Heres a link to MathWorks documentation that demonstrates the same in an example: https://in.mathworks.com/help/matlab/ref/annotation.html

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by