Code covered by the BSD License

### Highlights fromarrow.m

4.60241

4.6 | 86 ratings Rate this file 926 Downloads (last 30 days) File Size: 17.3 KB File ID: #278

# arrow.m

14 Jan 2000 (Updated 20 May 2009)

Draw a line with an arrowhead.

### Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

File Information
Description

ARROW (5/20/2009) Draw a line with an arrowhead.

ARROW(Start,Stop) draws a line with an arrow from Start to Stop (points should be vectors of length 2 or 3, or matrices with 2 or 3 columns), and returns the graphics handle of the arrow(s). By itself, ARROW will use the mouse to allow selection.

ARROW DEMO & ARROW DEMO2 show 3-D & 2-D demos of the capabilities of ARROW.

ARROW PROPERTIES shows the properties that can be set (length, base angle, tip angle, width, which ends have arrowheads, etc.)

Acknowledgements
MATLAB release MATLAB 6.5 (R13)
Tags for This File
Everyone's Tags
Tags I've Applied
30 May 2013
02 May 2013

Very useful, thanks. The help suggests using AXIS(AXIS) to fix the limits, I think the same effect can be acheived with AXIS MANUAL, which would be clearer.
It's also not obvious that the arrow will be a patch object, so has properties like edgecolor, facecolor, rather than line properties.

30 Mar 2013
07 Mar 2013

Perfect!

11 Feb 2013
10 Feb 2013

I wrote the following code:

figure(1)
clf
hold on;
scatter3(X,Y,Z,'ko')
Z1 = 500;
arrow([max(X) max(Y) Z1],[min(X) min(Y) Z1],15,'BaseAngle',60, 'FaceColor',[0 1 0])

I want to make outputs (which must contain the line along with arrow head) in X-Z plane; i.e. view([180 0]), not in 3D which is good for visualization only. Unfortunately, though the line is visible, the arrow head is not seen when I go into 2D view. I tried to include 'CrossDir',[0 0 1] as suggested by the author of the script, but it didn't help. Any suggestion?

20 Jan 2013

How do you turn off the arrow heads at either ends of the start and stop points?

17 Jan 2013

Very helpful and thorough! I only have a couple of minor quibbles - 1) the author didn't make it totally clear what normal MATLAB line properties you should use to change the arrow color and arrowhead color. But it's not too hard to figure out. 2) The author didn't assign message identifiers to the warnings (like for changing the axis limits). I edited the function to add message identifiers so I could turn them off - don't need my Command Window cluttered up with a lot of orange text.

12 Dec 2012
11 Oct 2012

Does anyone know how to add an arrow to the legend? I'm using arrow.m to draw arrows which represent data on the plot and it would be helpful to have a blue arrow in the legend.

11 Jun 2012

@Matthias, I had this problem too and just stumbled on a solution. (In fact, the problem for me was that the xlabel was disappearing.) To fix this I did:

ax1=subplot(2,1,1); [...]
axes(ax1); arrow([x1 y1],[x2 y2]); arrow fixlimits;

I always do "fixlimits" just in case. The reason I tried this was because in the help file:
"You may want to execute AXIS(AXIS) before calling arrow so it doesn't change the axes on you; arrow determines the sizes of arrow components BEFORE the arrow is plotted, so if arrow changes axis limits, arrows may be malformed."

It's not obvious that this fixed the subplot resizing problem, but it worked!

30 May 2012

By far the easiest way to draw arrow compared with arrow3.m and mArrow3.m.

14 May 2012
07 May 2012

Generally works fine. However, I use subplots, e. g. 3 x 2, and some of them have arrows. Now the arrows change the size of the subplot so that the subplots are no longer of equal size. Any idea how to correct that?

01 Apr 2012

How do I make the arrows dashed, and make the color for each arrow different, corresponding to its z value?

Adding the 'Linestyle',':' for dashed line option makes the arrows look rather strange.

For the colors, I tried creating an nx3 matrix lineColors where each row is a color for the corresponding element, and added the option " 'EdgeColor',lineColors ", but got an error:

"Color value must be a 3 element numeric vector"

Any help is appreciated. Thanks.

16 Mar 2012

exactly what I need. thanks a lot.

14 Feb 2012

Regarding W & Jacopo,

setting the view before the arrow functions solves your problem e.g.:

clc; clear; close all

xvector=[1 0 0];
yvector=[0 1 0];
zvector=[0 0 1];

figure
grid on
daspect([1 1 1])
xlabel('x [mm]', 'FontWeight', 'bold')
ylabel('y [mm]', 'FontWeight', 'bold')
zlabel('z [mm]', 'FontWeight', 'bold')

%Here goes the view setting
view([1 1 1])

arrow([0 0 0],xvector, 'EdgeColor','k','FaceColor','k')
arrow([0 0 0],yvector, 'EdgeColor','b','FaceColor','b')
arrow([0 0 0],zvector, 'EdgeColor','r','FaceColor','r')

%Not here
%view([1 1 1])

Don't no why but seems to work!

BW,
Tobi

29 Dec 2011

Wonderful. Thank you, Erik!!!

21 Dec 2011
01 Sep 2011

> Christopher This code, at least on >R2009a, looks very strange.
>close all; clc;
>figure(1)
>arrow([0;0;0],[1;0;0])
>arrow([0;0;0],[0;1;0])
>arrow([0;0;0],[0;0;1])
>axis([-1 1 -1 1 -1 1])
>I'm really having trouble with 3-D plots >and arrows pointing along the 3rd axis.

I got the same problem on Matlab R2010a (7.10.0)

31 Aug 2011

Using arrow with this script yields an unusually long arrow in the z direction, which is quite annoying. I wish the z arrow didn't become long.

clc; clear; close all

xvector=[1 0 0];
yvector=[0 1 0];
zvector=[0 0 1];

figure
grid on
daspect([1 1 1])
xlabel('x [mm]', 'FontWeight', 'bold')
ylabel('y [mm]', 'FontWeight', 'bold')
zlabel('z [mm]', 'FontWeight', 'bold')
arrow([0 0 0],xvector, 'EdgeColor','k','FaceColor','k')
arrow([0 0 0],yvector, 'EdgeColor','b','FaceColor','b')
arrow([0 0 0],zvector, 'EdgeColor','r','FaceColor','r')
view([1 1 1])

Help would be greatly appreciated.

19 Jul 2011

This is great, I messed around with the annotations for 1.5 hours before finding this. Once I found it everything was working the way I wanted it to in a couple of minutes. Great Work!

08 Jul 2011
19 May 2011

Perfect. Just what I was needing.

21 Apr 2011

Very nice utility function. I just wonder if it is possible to fix the arrow head size so that it does not change with axis- zoom or resize. Thanks!

18 Mar 2011

Really useful, thanks!

18 Mar 2011

Thanks :)

08 Mar 2011

Very nice, thanks

03 Mar 2011

can this function specify the 'LineStyle', such as '-' , '--' , '-.' , and ':'?
how to do?
thank you.

16 Feb 2011

thanks a lot. I was trying to make a arrow in a gui window. was having difficulty in correctly saving the arrow location. using your code it works perfectly :)

28 Jan 2011

Overall, the script is very useful. However, when using this arrow script, the legend is messed up. Some of the legend symbols would show as "black" squares instead of the correct symbol. When the arrow script is not used, the legend is displayed correctly.

02 Dec 2010

Could anybody please let me know the following:

1. How to change the size of the arrow?
2. Please let me know how to change the type of the arrow head.
3. If possible, please let me know how to have arrow at both the head and tail of the vector.
4. Instead of arrow, please let me know how I can have a dot at both ends of the vector?
5. How to change the line type?

Thanks.

12 Oct 2010

can you please include the color options that you write on these comments to the code help lines
thank you

12 Jun 2010

perfect!

19 May 2010

Works great, but the Arrowtips do not look perfect by default as they are the connection of two line ends and therefore not round in pdf printouts.
You can workaround this issue by drawing the arrow in the opposite direction and use parameter: 'End','start'.
But nonetheless it would be nice if they would be perfect by default ;-)

10 May 2010

THANK YOU!!!!

08 May 2010

This code, at least on R2009a, looks very strange.
close all; clc;
figure(1)
arrow([0;0;0],[1;0;0])
arrow([0;0;0],[0;1;0])
arrow([0;0;0],[0;0;1])
axis([-1 1 -1 1 -1 1])

I'm really having trouble with 3-D plots and arrows pointing along the 3rd axis.

04 May 2010

I found this implementation to be slow and lacking. Admittedly I was drawing thousands of arrows at extremely short lengths. Here is an inline function I wrote in about 5 minutes to draw simple arrows.

%by Ryan Molecke

theta = 22.5*pi/180;
theta1 = -1*22.5*pi/180;
rotMatrix = [cos(theta) -sin(theta) ; sin(theta) cos(theta)];
rotMatrix1 = [cos(theta1) -sin(theta1) ; sin(theta1) cos(theta1)];

v2 = v1*rotMatrix;
v3 = v1*rotMatrix1;
x1 = endpoint;
x2 = x1 + v2;
x3 = x1 + v3;
hold on;
% below line fills the arrowhead (black)
fill([x1(1) x2(1) x3(1)],[x1(2) x2(2) x3(2)],[0 0 0]);
% below line draws line (black)
plot([startpoint(1) endpoint(1)],[startpoint(2) endpoint(2)],'linewidth',2,'color',[0 0 0]);

17 Jan 2010

Useful in many ways but the code is very complicated and it's not clear how to attain specific effects without trawling through the function body. Why not put demo code in a separate file?

27 Dec 2009
15 Dec 2009
10 Nov 2009

It looks really great...I'll start use instead of quiver!

07 Oct 2009

amazing,very useful

20 Jul 2009

The arrowheads look distorted. Even the arrowheads in the demo look distorted.

16 Jun 2009

Excellent. One little problem is that the arrow created is a little different from the one inserted manually through the "insert" menu. For example, the position can not be adjust by mouse.

10 Feb 2009

to set the color of the arrow, see this example:
axis([0 3 0 3 0 3]);
grid on
h=arrow([3 3 3],[0 0 0],36,'BaseAngle',60);
set(h, 'FaceColor', 'b');
set(h, 'EdgeColor', 'r');
Awesome application!

30 Jan 2009

very useful.

29 Aug 2008
16 Aug 2008
15 Jul 2008
02 Jul 2008

the purpose of this Dir or format is for ?????

25 Jun 2008

OMG! Just exactly what I was looking for! Excellent job, thank you Eric!

10 Jun 2008

One can change the colour of an arrow by specifying 'FaceColor' and 'EdgeColor' properties, which is, unfortunately, not documented.

22 May 2008

good

27 Apr 2008
04 Feb 2008

Works great.

31 Oct 2007

sorry, how to add this graphic handle (return value) to an existing axes, figure.

24 Oct 2007

Does not work when called in a deployed application (i.e., when called from the executable generated using mcc).

09 Oct 2007

Ruins the figure when plotting 3-D arrows with z-components. In this case, the width of the arrowhead becomes so large that the plot is rendered unusable. This is the case no matter how one tries to set its size.

04 Oct 2007

works perfectly

08 Aug 2007
31 May 2007

Does exactly what it needs to do.

22 Apr 2007

Excellent and easy to use. I'd emphasize that one should call axis(axis) prior to plotting the arrow, or the axis limits will get reset which will render the arrow incorrectly. This is noted in the help, but tucked towards the bottom.

18 Apr 2007
29 Dec 2006

does not point to (1,1)

29 Dec 2006

Good job. A small remark:

arrow([0 0],[1 1],50,10,30)

does not point to (0,1).

Probably it would be reasonable to add a feature of automatical swapping baseAngle and tipAngle, if baseAngle < tipAngle.

30 Oct 2006

Great tool. Very easy to use with detailed documentation. Thanks for sharing it with us.

04 Oct 2006

this program is really good, easy-use. 3x Erik.

09 Sep 2006

The program is really great with all the aspects of plotting a directed line excellently covered.

24 Aug 2006

thanks eric

14 Mar 2006

This is great. I hate how MATLAB uses normalized figure coordinates in their annotation() call for drawing arrows. It makes no sense and someone at MathWorks got lazy, so it becomes nearly useless. This code is exactly what they should have done.. good job Erik!

14 Feb 2006

Nicely done. A shame by the way Matlab doesn't seem to have a builtin function for drawing arrows from the command line.

06 Feb 2006

Exactly what I needed. Perfect!

15 Dec 2005
22 Oct 2005

I add the following comment at the file head. arrow(Start,Stop,'EdgeColor','b','FaceColor','b') color arrow.

25 Sep 2005

Thanks for this excellent tool. I hope you continue writing such winderful codes!!!

18 Sep 2005

Works great, thanks!

13 Sep 2005

Very good. Thank you.

29 Aug 2005

Good script!

29 Jul 2005

This code (specifincally including the documentation (which is at the same time incomplete and inadequate) is far too complex for the simple thing(s) it does. I believe the author is an engineer: he should refrain from trying to write like a computer scientist and write like an engineer.

25 May 2005

Very good, better than annotation('arrow') because it is in axis units!

02 May 2005
20 Feb 2005

Couldn't figure out how to make the arrow line and arrow head have a specific color. Arrowheads didn't look as nice as those you might plot in drawing/presentation packages such as Powerpoint.

16 Jan 2005
13 Dec 2004

Superb effort. Highly configurable, easy to use and well documented to boot.

14 Oct 2004

Very useful!

06 Oct 2004

Excellent work, thorough documentation. Thanks!

06 Oct 2004

Excellent.

26 Jul 2004

Very useful. Explanations are excellent. Exactly what I was looking for.

09 Jun 2004

Very nice

06 May 2004

Very Useful and convenient. Easy to use!!

24 Feb 2004

Excellent package. Help topics very comprehensive. Easy to use. Quickly leads to appealing graphical output. This package is exactly what I was looking for.

05 Jan 2004

That's exactly what I'm looking for

12 Oct 2003

Neat package, but (unless I'm missing something obvious) renders itself essentially USELESS.
Try setting the linestyle of the arrow line to dotted (set(h,'linestyle',':') and then try printing it out to a file (print -depsc test.eps) and view the postscript image. The line certainly is not dotted (on Matlab 6.5). Same problem with dashed and dot-dashed lines.
I'm not sure if this is an arrow.m problem, or Matlab's.

03 Jul 2003

Great package, it should be incorporated in
the standard Matlab distribution

01 May 2003

It is just great. Amazing.

17 Feb 2003

Great!

02 Feb 2003
17 Dec 2002

greatest function ever!