Editor's Note: This file was selected as MATLAB Central Pick of the Week
ARROW3(P1,P2) draws lines from P1 to P2 with directional arrowheads. P1 and P2 are either nx2 or nx3 matrices. Each row of P1 is an initial point, and each row of P2 is a terminal point.
ARROW3(P1,P2,S,W,H,IP,ALPHA,BETA) can be used to specify properties of the line, initial point marker, and arrowhead. Type "help arrow3" at the command prompt for more information
What's new in Version 5:
All arrowhead and initial point marker sizes (W, H, and IP) are relative to the PlotBox diagonal.
Version 5 attempts to preserve the appearance of existing axes. In particular, ARROW3 will not change XYZLim, View, or CameraViewAngle. ARROW3 does not, however, support stretch-to-fill scaling. If a particular aspect ratio or variable limit is required, use DASPECT, PBASPECT, AXIS, or XYZLIM commands before calling ARROW3. Users are encouraged to report problems directly to the author.
Tom Davis (2021). Arrow3 (https://www.mathworks.com/matlabcentral/fileexchange/14056-arrow3), MATLAB Central File Exchange. Retrieved .
Inspired by: Arrow3 version 4.58
Inspired: JuGEx - Julich-Brain Gene Expression, mArrow3.m - easy-to-use 3D arrow, Minimum distance between two SuperEllipsoids., Particle Point Analysis, campodir
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
Hello, Mnar --
To use arrow3, the MATLAB current folder or MATLAB path must contain the file, arrow3.m. Please contact me through my MATLAB profile if you still have questions.
what I should do after downloading arrow3 tool? I have moved the file into R2016a yet I did not work for me, I keep getting the same error " arrow3 is unsigned function". help plz!
Awesome, thank you!
RE: UI Graphics in App Designer
14 Aug 2020 Joao De Lazzari
28 Mar 2019 Mike Potter
In R2020b, arrow3 works with the HandleVisibility property. In the tutorial, appdesigner.internal.application.openTutorial('BasicCoding'), for example, add the following to function AmplitudeSliderValueChanged(app, event).
value = app.AmplitudeSlider.Value;
plot(app.UIAxes, value*peaks)
app.UIAxes.YLim = [-1000 1000];
app.UIFigure.HandleVisibility = 'on';
figure(app.UIFigure); % set current figure
axes(app.UIAxes); % set current axes
pbaspect([133,56,5])
hold on
arrow3([0,-750],[value/2 - 0.01,-750],[],value/50)
hold off
app.UIFigure.HandleVisibility = 'off';
For earlier releases, arrow3 can be modified to accept figure and axes parameters.
function hn=arrow3(p1,p2,s,w,h,ip,alpha,beta,ax,fig)
...
% fig=gcf; ax=gca;
...
plot3(ax, ... ) % all instances
...
surface(ax, ... ) % all instances
Hello, Joao --
Is this related to Mike Potter's 28 Mar 2019 comment below? I confess that my reply may have been less than helpful. (I was very pleased with myself until all hell broke loose with R2019b.)
For the first time in many years, I have a recent version of MATLAB, and I would be interested in investigating how Arrow3 might support newer UI features. Please contact me via my MATLAB profile. Thanks.
Hello Tom,
First of all, the Arrow3 function is awesome! Thanks for sharing with us.
However I am having problems with plotting the arrows in a specific figure plot. Since I am using the AppDesigner, I have several figure axis plots to choose, and I must choose one when using Arrow3. But the function seems not to show this option yet (I am sorry if I am wrong about it).
There is any away that I can do this easily?
Best Regards
Version 5.18.1 addresses ColorOrder and LineStyleOrder changes in R2019b. This version has fewer problems but still suffers from some issues, particularly with regard to Update capabilities. More fixes will follow.
Hello, Chris --
Arrow3 takes its name from MATLAB functions like plot3, bar3, pie3, fill3, quiver3, etc. The arrowheads, initial point markers, and cones are always 3D surfaces. Using 2D coordinates will produce a 3D plot in the x-y plane with a 2D view.
Tom, It seems any arrow3 command turns a 2D plot into 3D. Not a big problem, but is this a fact of life when using arrow3 ? The help file implies using 2D coordinates will produce a 2D plot.
For example, even if I remove plot3() from your 19 June example it still outputs a 3D plot:
figure
p = 2*[rand(15,1), rand(15,1)];
plot(p(:, 1), p(:, 2), 'k.', 'MarkerSize', 15)
axis equal, hold on
plot(p(1:10, 1), p(1:10, 2), 'r.', 'MarkerSize', 30)
%plot3(p(1:10, 1), p(1:10, 2), ones(10,1), 'r.', 'MarkerSize', 30)
arrow3(p(1, 1:2), p(2, 1:2), '-b2', 2, 3)
hold off
Hello, Cameron --
Can you please contact me through my MATLAB profile and include your data with an example script? Thanks.
Hello Tom,
I'm having an issue with adding a legend to plots with arrows from arrow3. The legend picks up the line and the arrowhead as an entity, so since I have three arrows plotted, there are six entries in the legend. Three are regular lines, and three are the thicker blocks. Is there a way to fix this?
Thanks!
Hello Tom,
Me again. I stumbled upon something else (in 2D again). I found that the line from an arrow overlays the tip of the arrowhead. So the arrowhead is not sharp, but has a line end. I solved this by reducing the line length, depending on the arrowhead length (hacking the code).
Regards
Hello, Taco --
Your first comment does not appear in Comments and Ratings. I repeat it here for reference.
"Nice function. I stumbled upon something that I do not fully understand. When using in 2D, I have that other lines (plotted prior to arrow3) overlay the arrow heads (not fully, but just somewhere halfway the arrow head). I did not touch the alpha value. Any ideas what goes on?"
Arrow3 5.17 specified renderer, Zbuffer, for plots without explicit z coordinates.
"[Zbuffer] renders only those pixels that are visible in the scene (thus eliminating front-to-back sorting errors)."
But Zbuffer is now deprecated, and Arrow3 5.18 uses the Opengl renderer for all plots.
"The OpenGL renderer displays objects sorted in front to back order, as seen on the monitor. Lines always draw in front of faces when at the same location on the plane of the monitor."
So your solution ("This issue seems to be solved when adding a positive z-coordinate.") is a good workaround.
-------------------------------------------------
Hello, Chuan --
See comments to and from Taco dated 19 Jun 2020, re renderers. Following code consistently plots red points on top of black points. Note that arrow3 is called last.
figure
p = 2*[rand(15,1), rand(15,1)];
plot(p(:, 1), p(:, 2), 'k.', 'MarkerSize', 15)
axis equal, hold on
plot3(p(1:10, 1), p(1:10, 2), ones(10,1), 'r.', 'MarkerSize', 30)
arrow3(p(1, 1:2), p(2, 1:2), '-b2', 2, 3)
hold off
This issue seems to be solved when adding a positive z-coordinate.
Hello, Linjiang --
The following assumes that z1 (size) is the cone width. By EdgeColor (z3), you likely mean the cone outline in 2D, but arrow3 cones are always 3D surfaces with 21 (0:20) faces. Hence, outlining cannot be achieved with EdgeColor. This code produces a cone plot that responds to colormap changes:
p1 = p(:,1:2);
heading = pi*p(:,3)/180;
p2 = [sin(heading), cos(heading)];
h = arrow3(p1,p2,[],p(:,4),[],'cone');
% Change truecolor CData to colormap values.
c = p(:,5);
for n = 1:length(c)
set(h(n),'CData',c(n)*ones(2,21))
end
caxis('auto')
Hello, Pedro --
As documented in the Overview above, "If a particular aspect ratio or variable limit is required, use DASPECT, PBASPECT, AXIS, or XYZLIM commands before calling ARROW3." The reason for this is that ARROW3 draws surfaces that look like right circular cones and spheres no matter the aspect ratio, but the ratio must be fixed, and changing that ratio will distort the surfaces.
[bug] It seams that arrow3 fixes the axis limits for some reason.
Hi, I have used arrow3 for a long time, when I used it in matlab r2014b and r2017b, it's ok, there is no problem. However, when I used the same code in r2019b, the result is not the same as before and it's wrong. For example,
figure, plot(p(:, 1), p(:, 2), 'k.', 'MarkerSize', 15);axis equal, hold on,
arrow3(p(1, 1:2), p(2, 1:2), '-b5', 0.2, 0.3);hold on,
plot(p(1:10, 1), p(1:10, 2), 'r.', 'MarkerSize', 20);
although point size in the second plot is larger than in the first plot, the black dots can't be covered by the red color set in the second plot.
Hello Tom
we hope to draw cones with colormap. Do you have any example of this? Example 2 and Example 6 work together(https://public.brain.mpg.de/Laurent/SheinIdelsonetal2017/NSKToolbox/GraphicsPlotting/arrow3/arrow3_examples.html). For example, we have data with 5 rows:x,y,heading angle, z1,z2.
x and y denote are the coordinate, heading angle is the direction, z1 denotes the size, z2 denotes the color, which should use colormap. (It would be better if we have additional raw z3 to control the EdgeColor)Can you help me, follow is our data:
115.700 21.600 111.415 1.051 0.892
116.600 22.800 74.331 0.848 0.151
115.300 23.100 49.346 0.826 0.133
114.400 23.000 16.858 0.798 0.168
116.400 21.000 118.786 0.501 0.274
116.500 22.000 94.239 0.502 0.245
116.700 22.900 72.636 0.509 -0.198
115.300 20.100 152.103 1.247 0.249
113.900 20.200 186.223 1.244 0.412
112.700 20.500 218.671 1.244 0.359
112.000 21.000 239.609 1.237 0.119
115.300 20.100 152.103 1.298 0.947
114.100 20.300 180.858 1.278 0.865
113.100 20.400 208.520 1.253 0.561
112.400 20.300 220.937 1.228 -0.285
114.300 19.700 176.306 1.056 0.514
114.100 20.400 180.905 1.050 0.547
113.700 21.200 202.261 1.047 0.804
113.200 22.100 264.870 1.048 0.504
112.900 22.500 285.943 1.043 0.348
Hello, Allen --
As documented in the Overview above, and consistent with PLOT3, "P1 and P2 are either nx2 or nx3 matrices. Each row of P1 is an initial point, and each row of P2 is a terminal point." If column vectors are more convenient, you can transpose them, e.g. arrow3([0;0;0]',[1;1;1]').
This only works if the start and end vectors are rows (1x3). Given 2 column vectors, it returns 'Invalid input, type HELP ARROW3 for usage examples '
Hello, Chris --
Version 5.17 does not adequately address the line color issue. I will post an interim fix that I hope will meet most needs, but the code still has a lot of problems. Arrow3 makes extensive use of the ColorOrder property, and that property has changed fundamentally in 2019b.
I've been using arrow3 for some time, but suddenly I cannot control line colors. For example:
arrow3(axesTails,axesHeads,'l-.',0.6)
used to produce light grey arrows, but now they are blue.
(Also happens for my older scripts that used to work fine)
I'm using MATLAB 2019b
Is anyone else experiencing this?
Hello, Li --
It is most likely that the problem is the position of the last line of code. From the Overview above, "If a particular aspect ratio or variable limit is required, use DASPECT, PBASPECT, AXIS, or XYZLIM commands before calling ARROW3." See also arrow3_messes_up_my_plots.html in arrow3.zip.
Hello, Li --
I do not have wind.dat. Please contact me through my MathWorks profile, and we'll sort it out.
First, I use quiver to get P1 and P2 of the arrows. Then I use Arrow3 to draw the arrows. I found that the base of the head is not perpendicular to the line. Can you please help me to solve this. My code is as follows:
data=load('wind.dat');
x=data(:,1);
y=data(:,2);
u =data(:,3);
v =data(:,4);
%// Taken from "quiver.m". Computes autoscale
if min(size(x))==1, n=sqrt(numel(x)); m=n; else [m,n]=size(x); end
delx = diff([min(x(:)) max(x(:))])/n;
dely = diff([min(y(:)) max(y(:))])/m;
del = delx.^2 + dely.^2;
if del>0
len = sqrt((u.^2 + v.^2)/del);
maxlen = max(len(:));
else
maxlen = 0;
end
if maxlen>0
autoscale = 0.9 / maxlen;
else
autoscale = 0.9;
end
%// quiver plot
quiver(x, y, u, v)
p1=[x,y];
m=autoscale*u;
n=autoscale*v;
arrow3(p1,p1+[m,n],'k',0.18,1.5)
axis([1 28 -1.5 1.5]), daspect([8 2 1])
Version 5.17 addresses line color issues introduced in R2019b.
Thank you for this function. Please rate after reading the examples in html in zip of this download.
Thank you for the bug report, David.
There is no intentional mechanism to set arrowhead color independent of line color. I believe the line color issue is related to new color and line style features in R2019b. I have made preliminary changes to the code that support single-colored lines and arrows, e.g. your example, but multicolored lines and arrows are still a problem. See arrow3_examples.html and attendant images in arrow3.zip for examples of intended results.
Contact me through my MathWorks profile, and I will keep you updated as I modify Arrow3.
Matlab r2019b: Color specification has no effect on line color
source code indicates line color using plot3 is not set whatsoever - why would I want to set the arrowhead color independent of the line?
ex.
arrow3(-eye(3),eye(3),'k',1.25,[],0)
yields 3 unit vectors with black arrow heads and blue lines for me, plot3 uses the default color order which I don't want to modify globally
Hello, Ali --
ALPHA (default = 1) is a vector of FaceAlpha values ranging between 0 (clear) and 1 (opaque). The number of pixels (resolution) does not change with decreasing ALPHA, but the pixels themselves become more transparent.
Changing "ALPHA" decreases the resolution of arrows. Why?
Hello, J.P. --
The column vector of decimal values *is* a vector of handles. See comment on 26 Jul 2011 below, re animation.
How do you remove arrow3 objects from a figure during a looped animation? For some reason hn = arrow3(p1,p2,...) does not return a vector of handles to line and surface objects. In fact, hn is simply a column vector of decimal values. Any help would be greatly appreciated.
Hello, Mike --
Arrow3 is 17 years old; it is a testament to MATLAB's backward compatibility that it works at all. Arrow3 always plots to the current figure (gcf) and axis (gca), but it is likely that you can change this behavior by modifying line 349: fig=gcf; ax=gca;
How do you cause the arrow3 to draw to a particular axis handle? I'm trying to use it in a GUI (a UIaxes), but the function doesn't seem to allow you to direct it to a particular axes. And the command 'axes(...)' doesn't work with UI axis
Is there an easy way to specify which axis to plot the arrows to? I am trying to use in the a matlab app and the axis must be specified.
It is great. However. the quality of its emf file is low. BTW: I outport the emf file by "save as".
Great function, works perfectly for me.
How do I handle this in the legend, though? I have
eq=arrow3(... first set of arrows
lq=arrow3(... second set of arrows
legend([eq lq],'normals from edges','normals from layers')
and it's giving me an error ("Operands to the || and && operators must be convertible to logical scalar values.")
Thanks for any advice!
You're so nice guy~
Thank you Tom
Hello, Ikem --
Extract the arrow3.zip files to a folder, e.g. C:\arrow3.
Then in the MATLAB Command Window:
cd C:\arrow3
help arrow3
Please how do I install it. I am having problems attaching it to my matlab
Hello, John --
This works for me:
load durer
image(X)
colormap(map)
axis image
hold on
arrow3([235 215],[130 465],'r-2.5',1.5)
hold off
Arrow head not showing if plotted over an image?
Thank you Tom!
Hello Tom,
Sorry I make a mistake.I mixes up lon and lat.I can draw arrows on worldmap now.You can ignore my question earlier.Thank u very much!
Best wishes.
Liya
Hello Tom,
Thank you for your answer.When I draw 2D arrows,which plotting functions in your code make it work? I just find plot3.Could you please tell me all plotting functions you used in your code .
--Liya
Hello, Liya --
I have neither the Mapping Toolbox nor a recent version of MATLAB. If you can ask specific questions, I may be able to help.
Good luck and best wishes.
-- Tom
hello Tom,I want to ask you a question.When I use the function worldmap and draw arrows on it,I try to modify your code,but I can't.Could you please modify your code so that it can be used on the worldmap.Thank u so much!
Thanks a lot! The function is very useful
Nice, saved me from a lot of footwork.
I love this .m file.
sorry i didn't want to submit the last voting. I didn't read the "relative to the PlotBox diagonal" at the time i was about to write a critic, but as i reread the description i closed the tab.
But i still think that this is an annoying feature, i would like it more if i could choose wether it should be relative or a fix size. I also don't like that it clears the axes. I know a workaround but it's still annoying me everytime i have to use arrow3. Just my opinion.
Thanks for your arrow3 program.
Great code. Have been using it for quite a long time.
Hi, Jonas --
Please try version 5.15. Thanks.
-- Tom
I've downloaded arrow3 sometime in 2002 for the first time; great job, guys!
In order to avoid possible conflicts with other code writing to the 'UserData' property, I suggest replacing these calls by "setappdata(ax,'arrow3',value)"
It does not ignore zero magnitue input (R2012a).
>> arrow3([1 1],[1 1])
Error using arrow3 (line 373)
P1 cannot equal P2
Thanks!!!
Hi Tom,
thanks for that nice contribution. I would enjoy if you could modify the input arguments with string propertys (similar to most of the inbuilt-functions (f.e. 'LineWidth', 2)), in particular to add a 'Parent' property to get rid of the problems with multiple axes!
Thanks,
Daniel
Hello, Tomazc --
Please try this and write if you have questions. Thanks.
% Animation Example
figure(5)
t=(0:pi/40:8*pi)'; u=cos(t); v=sin(t);
plot3(20*t,u,v)
axis([0,600,-1.5,1.5,-1.5,1.5])
grid on, view(35,25)
hold on
pbaspect([1.8,1.4,1])
arrow3(zeros(3),diag([500,1.5,1.5]),'l',0.7,[],0)
p=[20*t,u,v]; inc=4:1:length(t);
p2=p(inc,:); p1=p(inc-1,:);
hn=arrow3(p1(1,:),p2(1,:),'0_b',0.7);
for i=2:1:length(p1)
delete(hn)
hn=arrow3(p1(i,:),p2(i,:),'0_b',0.7);
pause(0.01)
end
hold off
I'd like to use arrow3 in animation (for function) where only one arrow will be shown, so i cannot use 'hold on'.
In this case the 'axis' are not working, but i need the same view.
Any suggestion?
figure('Name',['Arrow'])
axis([-400 400 -400 400 -100 700])
XLABEL('x')
YLABEL('y')
ZLABEL('z')
whitebg([1 1 1]);
set(gcf,'Color',[1,1,1]);
view(35, 25)
grid on
for i = 1:1:15
arrow3([x(i),y(i),z(i)],[fx(i),f(i),fz(i)]);
pause(0.1)
end
Does what it's supposed to do, and the help files are VERY detailed. Thank you very much! :)
Instead of using Copy Figure from the Edit menu, use Save As from the File menu, save the figure as a PNG file, and then insert the PNG file into Word.
Very useful with 3D plots. But when I'm copying figures to word, all the nice arrows plotted by 'arrow3.m' disappeared. And my command window displays:
Warning: RGB CData not yet supported in Painter's mode
> In graphics\private\restorehg at 100
In graphics\private\restore at 29
In print at 277
In editmenufcn at 78
How do I fix this?
Wide lines may obscure the arrowhead tip, but the line endpoint and arrowhead tip are _always_ coincident.
On my matlab (R2007a) the line of the arrow is visible after the tip of the arrowhead in 2D plots. It looks like as if the arrowhead is plotted at the wrong position.
In my example arrow3 is called after some calls of "line()" and "rectangle()". I can supply further information if required.
It is not necessary to set AXIS EQUAL, but commands that may change the aspect ratio should be issued _before_ calling ARROW3. ARROW3 draws surfaces that look like right circular cones and spheres no matter the aspect ratio, but the ratio must be fixed, and changing the ratio will distort the surfaces.
There are many examples in the HTML files that illustrate how to use ARROW3 before and after PLOT, POLAR, CONTOUR, PLOT3, SURF, SLICE, and PATCH.
it seems if I want to plot another plot3(......) with arrow3(.....)
I should
figure,arrow3(.....);
hold on;
axis equal ; %you'd better use axis equal
plot3(....);
if I put plot3(....) before arrow3(.....) ,just like below ,I get not see the result of arrow3(.....);
figure,plot3(....);
hold on;
axis equal ; %you'd better use axis equal
arrow3(.....);
I am doing a project in layup optimization of balanced symmetric laminates, and I wanted to display at each call to fmincon the material direction for my laminate, I was able to so by using arrow3, thank you.
Regards,
Vaibhav Deshpande.
works fine
it's excellent.I can use it to draw my picture. thanks!
I was happily using arrow.m until I came to see that it for some reason did not draw anything when my m file was deployed (i.e., running executable that resulted from mcc). I came back to File Exchange for a different arrower instead of trying to debug arrow.m. Now I'm using arrow3 which works fine in deployed mode. thanks.
I havent tried it but looks good from the screen shot, what was the thought process on this one, and how long did it took you