2
votes

I was wondering if it was possible for someone to provide me with some code examples for working with scattered XYZ point data in MATLAB Curve Fitting Toolbox? I would like to fit a surface to points that approximate a cylinder.

Thanks.

1
Have no experience, but have you looked at the links from the Matlab Surface fitting page?Steve

1 Answers

4
votes

In Matlab R2015b and above, You can use pcfitcylinder to fit a cylinder to a pointCloud object. Let's start with producing an example data to see how it works:

[theta, r, h] = meshgrid(0:.1:6.28, 1, 0:.2:4); % making a cylinder
r = r + 0.05 * randn(size(r)); % adding some radial noise
[x, y, z] = pol2cart(theta, r, h); % transforming the coordinate system
P = (rotx(60) * [x(:), y(:), z(:)]')'; % rotating the data points around x axis
figure;
scatter3(P(:, 1), P(:, 2), P(:, 3))
axis equal

enter image description here

The point cloud object is created as follows:

ptCloud = pointCloud(P);

Then the model can be fitted:

maxDistance = 0.02;
model = pcfitcylinder(ptCloud, maxDistance);
hold on
plot(model)

enter image description here

Depending on your data, to get a reasonable cylinder you might need to take a look at pcfitcylinder and consider including other input arguments.