简体   繁体   中英

Connecting points on a surface plot in matlab

I am working on a project where we make measurements in a linear fashion and rotate the sample in order to produce a 3D surface map.

The data comes from a polar format, we have 4 datasets:

theta = 0  , r = -20 -- 20
theta = 45 , r = -20 -- 20
theta = 90 , r = -20 -- 20
theta = 135, r = -20 -- 20

I can transform this into cartesian points by generating x and y vectors with sin and cos and plotting the result against that matrix.

So an example "ready to plot" matrix set looks something like this:

x [4x21 double]
-20    -18    -16 ..
-14.14 -12.73 -11.31 ..
0      0      0 ..
..

y [4x21 double]
0      0      0 ..
-14.14 -12.73 -11.31 ..
-20    -18    -16 ..
..

z [4x21 double]
.224 .281 .320 ..
.228 .280 .312 ..
.282 .349 .377 ..
..

The output surface plot with some basic formatting is like this: 以上数据的笛卡尔曲面

Matlab is connecting between the vectors which I want. However, it is not connecting the 90-degree rotation to the 135-degree rotation. How do I make it do this?

The basic code to produce a figure similar to the example above is this:

theta = [ 0 , -45 , -90 , -135];
r = -20 : 2 : 20;
for i = 1 : 4
    xc(i,:) = r .* cosd(theta(i));
    yc(i,:) = r .* sind(theta(i));
    zc(i,:) = [[0 : 0.1 : 0.9] , [1 : -0.1 : 0]];
end
figure
surf(xc,yc,zc);

You just have to do theta=-180 explicitly. Just like plot doesn't wrap-around and join the first and last points, surf wont connect the first "edge" to the last "edge". Make theta=[0 -45 -90 -135 -180] and do for i=1:5 and it should be good.

在此处输入图片说明

You'll find the colouring looks a bit weird, this might look better depending on the application: surf(xc,yc,zc,'FaceColor','interp') .

在此处输入图片说明

A better way to create your data could be

[R,TH]=meshgrid(r,theta);
[xc,yc]=pol2cart(TH*pi/180,R);
zc = repmat([[0 : 0.1 : 0.9] , [1 : -0.1 : 0]],length(theta),1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM