Code covered by the BSD License  

Highlights from
arrow.m

4.60674

4.6 | 92 ratings Rate this file 880 Downloads (last 30 days) File Size: 17.3 KB File ID: #278
image thumbnail

arrow.m

by

 

14 Jan 2000 (Updated )

Draw a line with an arrowhead.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

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

This file inspired Display A Curve With Quaternions, The Carpetplot Class, Random Boolean Network Toolbox, Arrowflow, Quiver2.M V1.2 (Nov 2009), and Generalized States Of Plane Stress And Plane Strain.

MATLAB release MATLAB 6.5 (R13)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (105)
04 Mar 2014 Pegah Hosseini

I think this is one of the best functions I have ever stumbled upon. Thanks alot.

05 Feb 2014 Yagiz  
08 Jan 2014 Li Zhang

Useful! How to change the color of the arrow ?

17 Oct 2013 Alan  
13 Aug 2013 ammar al jodah  
11 Jul 2013 Harish Babu Kankanala  
30 May 2013 Alexandros Iliopoulos  
02 May 2013 Jo Williams

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 Jan Motl  
07 Mar 2013 Jan Valdman

Perfect!

11 Feb 2013 Sisi Ma  
10 Feb 2013 Rajib Mozumder

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 Michael Chan

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

17 Jan 2013 Matthew M.

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 Haochen Tang  
11 Oct 2012 Ryan

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 Kent Leung

@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 Felix

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

14 May 2012 Jiangmin zhang  
07 May 2012 Matthias Hunstig

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 ATN

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 Yuanye Wang

exactly what I need. thanks a lot.

14 Feb 2012 Tobias

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 John Colby

Wonderful. Thank you, Erik!!!

21 Dec 2011 Kuifeng  
01 Sep 2011 Jacopo

> 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 Will

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 William Mische

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 Dmytro Lituiev  
19 May 2011 Marco idiart

Perfect. Just what I was needing.

21 Apr 2011 Zhibo

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 Richard Crozier

Really useful, thanks!

18 Mar 2011 Miguel Gil-Pugliese

Thanks :)

08 Mar 2011 Arthur

Very nice, thanks

03 Mar 2011 guizhi

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

16 Feb 2011 Rishabh Kasliwal

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 CC

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 hajime yamada

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 ben harper

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

12 Jun 2010 Jiangmin zhang

perfect!

19 May 2010 Notan

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 David

THANK YOU!!!!

08 May 2010 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.

04 May 2010 Ryan Molecke

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.

function out = draw_arrow(startpoint,endpoint,headsize)
%by Ryan Molecke

v1 = headsize*(startpoint-endpoint)/2.5;

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 Rob Campbell

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 Aamir  
15 Dec 2009 Aamir  
10 Nov 2009 Lorenzo Guerrasio

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

07 Oct 2009 never change

amazing,very useful

20 Jul 2009 Gabriel Akira Schreiber

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

16 Jun 2009 Jianbo

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 Hiram

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 Ke

very useful.

29 Aug 2008 Saeed Taherion  
16 Aug 2008 Leonardo Duarte  
15 Jul 2008 lili kalmer  
02 Jul 2008 Joselito Dimayuga

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

25 Jun 2008 Dmitry Vasilyev

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

10 Jun 2008 GDR! x

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

22 May 2008 Xuzhang Shen

good

27 Apr 2008 Alex Fard  
04 Feb 2008 Matthijs Klomp

Works great.

31 Oct 2007 Tuan Hoang

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

24 Oct 2007 Ahmet Sacan

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

09 Oct 2007 Rutty Bee

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 Dustin Kruse

works perfectly

08 Aug 2007 Jorge Marin  
31 May 2007 B Gurt

Does exactly what it needs to do.

22 Apr 2007 jonny e

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 Ramesh Rajasekaran  
29 Dec 2006 Vassili Pastushenko

does not point to (1,1)

29 Dec 2006 Vassili Pastushenko

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 Arun Karthi Subramaniyan

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

04 Oct 2006 J Xu

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

09 Sep 2006 Nithya C.R.

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

24 Aug 2006 heath kitson

thanks eric

14 Mar 2006 JR P

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 Jasper Menger

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 Very satisfied

Exactly what I needed. Perfect!

15 Dec 2005 super tanne  
22 Oct 2005 Shane Lin

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

25 Sep 2005 Vijaykumar Sathyamurthi

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

18 Sep 2005 Sharoni Shafir

Works great, thanks!

13 Sep 2005 Amit Kesar

Very good. Thank you.

29 Aug 2005 Changshun Deng

Good script!

29 Jul 2005 Dave Cogdell

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 Janik Zikovsky

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

02 May 2005 Ian Mackenzie  
20 Feb 2005 Evan Westwood

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 Rattikan Saelim  
13 Dec 2004 Nathaniel de Lautour

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

14 Oct 2004 Olivier Marsden

Very useful!

06 Oct 2004 Eddie Fiorelli

Excellent work, thorough documentation. Thanks!

06 Oct 2004 Mattias Forsblom

Excellent.

26 Jul 2004 Kelvin Rocha

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

09 Jun 2004 Gerald Dalley

Very nice

06 May 2004 ke wang

Very Useful and convenient. Easy to use!!

24 Feb 2004 Arnd-Ragnar Rhiemeier

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 Bernhard Boeck

That's exactly what I'm looking for

12 Oct 2003 Benedikt Halldorsson

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 Jakob van de Laar

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

01 May 2003 M Bakht

It is just great. Amazing.

17 Feb 2003 Niclas Borlin

Great!

02 Feb 2003 Aslak Grinsted  
17 Dec 2002 a c

greatest function ever!

Updates
20 May 2009

Replace "eval('trycmd','catchcmd')" with "try, trycmd; catch, catchcmd; end;", which break's compatibility with MATLAB 5 but improves compliance with newer versions.

20 May 2009

actually update the file (not just the description); correct view on demo

Contact us