Error in plot3: Vectors must be the same length

Hey, so I'm trying to use this function to plot a 2D circle on a 3D space, but I get this annoying error every time and I can't understand why:
function plotcircle(ax,x,y,z,r,color)
ang=0:0.01:2*pi;
xp=r*cos(ang);
yp=r*sin(ang);
zp = z*ones(1, length(ang));
if nargin == 6
plot3(ax, x+xp,y+yp, zp, 'Color',color);
else
plot3(ax, x+xp,y+yp, zp);
end
I've checked, and all 3 vectors are the same length and have 629 elements. Am I missing something?
Thank you.

4 Comments

At the time the problem occurs, what is size(xp), size(yp), size(zp), size(x), size(y), size(z) ?
I suspect x or y are column vectors, and since xp and yp are row vectors, row vector plus column vector gives a 2D array.
x, y, and z are numbers. they are the center of the circle.
plotcircle(axis(), 1, 2, 3, 4, 'r')
size(x+xp)
size(y+yp)
size(zp)
Returns:
ans =
1 629
ans =
1 629
ans =
1 629
You were passing in the result of axis() instead of axes()
plotcircle(axes(), 1, 2, 3, 4, 'r')
function plotcircle(ax,x,y,z,r,color)
ang=0:0.01:2*pi;
xp=r*cos(ang);
yp=r*sin(ang);
zp = z*ones(1, length(ang));
if nargin == 6
plot3(ax, x+xp,y+yp, zp, 'Color',color);
else
plot3(ax, x+xp,y+yp, zp);
end
end
Ah * facepalm *
Thanks, it's working now :)

Sign in to comment.

Answers (0)

Asked:

on 18 Jan 2021

Commented:

on 18 Jan 2021

Community Treasure Hunt

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

Start Hunting!