Extract data for 3d plated grave
Show older comments
Hello everyone!
i just started learing mathlab. i did plot a simple 3d graph of circles by this code ;
clear;
clc;
figure(1)
r=3;
th=0:180/30:360;
Lgt=length(th);
for height=0:4:20;
x=r*cos(th);
y=r*sin(th);
z= height* ones(r,Lgt);
plot3(x,y,z)
hold on
r=r+5;
end
hold off
when i came to extract the ploted graph 3d data i tried different way among them
ch=get(gca,'children')
X=get(ch,'Xdata')
Y=get(ch,'Ydata')
Z=get(ch,'Zdata')
which resulted in many many date, like repeated 20 20 20 many times while it should just gave me on raw of 20s one of 16s...0s
the same with x and y .
how to extract them correctly?
thanks in advance
Answers (1)
Let's first run your code:
figure(1)
r=3;
th=0:180/30:360;
Lgt=length(th);
for height=0:4:20;
x=r*cos(th);
y=r*sin(th);
z= height* ones(r,Lgt);
plot3(x,y,z)
hold on
r=r+5;
end
hold off
ch=get(gca,'Children')
X=get(ch,'XData')
As you can see, each circle in your plot is a separate line object. The get function will return a cell array with a cell element for each line. Since the x and y don't actually depend on your loop variable (and therefor could be computed outside the loop), you can just select one of them.
The ZData property contains 1 z value for every x and y. Otherwise it wouldn't be possible to plot them. If you're only after a single value, you could either select 1 value at random, or use unique (which will allow you to detect multiple heigths).
Z=get(ch,'ZData');
for n=1:numel(Z)
Z{n}=unique(Z{n});
end
Z
Categories
Find more on 2-D and 3-D Plots 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!