How to plot an arrow with a point, length and direction?
8 views (last 30 days)
Show older comments
Hi.
I want to plot an arrow with a starting point (x,y), length and direction(degree), but there shouldn't be cosinus and sinus commands in code. So, at the end of the process, the length should be the same. When I use cos and sin commands to define endpoint (xend,yend), the length is changing.
How can I do that? (I tried arrow and quiver commands but I'm failed)
Thanks for your answers.
2 Comments
Walter Roberson
on 13 Feb 2014
Edited: Walter Roberson
on 13 Feb 2014
annotate() ?
If the length is changing, is it possible that you have not done
axis equal
?
Image Analyst
on 14 Feb 2014
I moved what poster said in an "Answer" to a comment here since it's not an "Answer" to his original question:
Walter Roberson, thanks for your answer. But I can't understand what did you mean.
In my opinion, the problem is not about the axis. Because I can't find a command that I can direction, length and point, all them together except for arrow command.
If you know anything about this that I can, could you give me a sample code?
Thanks again.
Answers (3)
Walter Roberson
on 13 Feb 2014
MATLAB does not have an arrow() command. Arrows are created using
annotation('arrow', x, y)
You cannot set direction by angle; you need to give x and y coordinates. You can use pol2cart() to do the transformation:
[delta_x, delta_y] = pol2cart(length, direction_angle);
annotation('arrow', [base_x, base_x + delta_x], [base_y, base_y + delta_y] )
2 Comments
DGM
on 12 Nov 2021
Edited: DGM
on 12 Nov 2021
The mistake in this answer is that the order of the arguments to pol2cart() are swapped.
[x y z] = peaks(100);
pcolor(x,y,z);
shading flat
base_x = 0;
base_y = 0;
len = 0.707;
th = pi/4;
[delta_x, delta_y] = pol2cart(th,len);
annotation('arrow', [base_x, base_x + delta_x], [base_y, base_y + delta_y] )
This might also demonstrate what may be another misconception. The annotation is being placed in normalized figure coordinates, not data coordinates. Since you haven't described the problem you're having, that might be part of it.
Tord Vincent Heggland
on 4 Mar 2020
[delta_x, delta_y] = pol2cart(length, direction_angle);
annotation('arrow', [base_x, base_x + delta_x], [base_y, base_y + delta_y] )
1 Comment
Majid
on 12 Nov 2021
I guess this does not work because the input of annotation() should be between 0 and 1. Am I right?
See Also
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!