How do I make a 3d scatter plot?
7 views (last 30 days)
Show older comments
I keep getting an error message about "vector not same length"
function [ output_args ] = scat( xstart,ystart )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
n=3;
for m=0:n
x=xstart+1*m;
y=ystart+1*m;
xax=xstart:0.5:xstart+n*0.5
yax=ystart:0.5:ystart+n*0.5
end
for m=1:n+1
z(m)=m;
end
figure
plot([xax,yax],z,'.')
end
I am trying to create a very easy scatter plot. Is there also a a way to look at the cross-section of the 3d scatter plot?
0 Comments
Answers (3)
mahesh babu
on 18 Sep 2016
Edited: mahesh babu
on 18 Sep 2016
try using fplot instead of plot
fplot([xax,yax],z,'.')
4 Comments
mahesh babu
on 18 Sep 2016
try pasting this, if you still get the error, then remove xstart and ystart and replace them with constants
Walter Roberson
on 18 Sep 2016
You have
for m=0:n
x=xstart+1*m;
y=ystart+1*m;
xax=xstart:0.5:xstart+n*0.5
yax=ystart:0.5:ystart+n*0.5
end
None of your variables x, y, xax, yax, are indexed at m (or a value derived from m), so each loop through you are overwriting all of the previous values. The effect is exactly the same as if you had only done the last iteration,
m = n;
x=xstart+1*m;
y=ystart+1*m;
xax=xstart:0.5:xstart+n*0.5
yax=ystart:0.5:ystart+n*0.5
It seems unlikely that is what you want.
For a 3D scatter plot, you should be using scatter3(), something similar to
scatter3(xax, yax, z)
0 Comments
Star Strider
on 18 Sep 2016
If you have three arguments, you need to substitute plot3 for plot.
Try this:
function [ output_args ] = scat( xstart,ystart )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
n=3;
for m=0:n
x=xstart+1*m;
y=ystart+1*m;
xax=xstart:0.5:xstart+n*0.5;
yax=ystart:0.5:ystart+n*0.5;
end
for m=1:n+1
z(m)=m;
end
figure
plot3(xax,yax,z,'.')
grid on
end
With my changes, the internal part of your code runs for me without error, and produces the plot. I leave it to you to determine if it produces the correct result.
Note that you do not assign ‘output_args’ in your function, so that will throw an error:
Error in [calling script] (line [line #])
[ output_args ] = scat( xstart,ystart )
Output argument "output_args" (and maybe others) not assigned during call to
"ScratchPad/scat".
You have to decide what you want your ‘scat’ function to return.
2 Comments
Star Strider
on 18 Sep 2016
You can do it manually in the plot GUI, or if you already know the orientation you want, use the view function. (It’s easier to do it with the plot GUI first, then later fix the view in your program with the view function. That’s what I usually do. In the plot GUI, the azimuth and elevation are in the lower left corner of the plot figure.)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!