How to plot an arrow with a point, length and direction?

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

annotate() ?
If the length is changing, is it possible that you have not done
axis equal
?
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.

Sign in to comment.

Answers (3)

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

I guess this does not work because the input of annotation() should be between 0 and 1. Am I right?
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.

Sign in to comment.

[delta_x, delta_y] = pol2cart(length, direction_angle);
annotation('arrow', [base_x, base_x + delta_x], [base_y, base_y + delta_y] )

1 Comment

I guess this does not work because the input of annotation() should be between 0 and 1. Am I right?

Sign in to comment.

Don't you have "direction" and "length" interchanged?

Tags

Asked:

on 13 Feb 2014

Edited:

DGM
on 12 Nov 2021

Community Treasure Hunt

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

Start Hunting!