## How to draw a cylinder that has an irregular cross section and changing along the cylinder length?

Muhamed Sewidan

### Muhamed Sewidan (view profile)

さんによって質問されました 2019 年 3 月 21 日

### Muhamed Sewidan (view profile)

さんによって コメントされました 2019 年 3 月 22 日
John D'Errico

### John D'Errico (view profile)

さんの 回答が採用されました
I need to draw a cylinder that has a different radius around the rotation axis, and these radii change along that cylinder, these data may be helpful to get the point.

#### 2 件のコメント

2019 年 3 月 21 日
What's preventing you from creating multiple cylinders and then concatenating them together graphically?
Muhamed Sewidan

### Muhamed Sewidan (view profile)

2019 年 3 月 21 日
how can I concatenate them?

サインイン to comment.

## 1 件の回答

2019 年 3 月 21 日

### John D'Errico (view profile)

2019 年 3 月 22 日
採用された回答

You essentially have radius, as a function of theta, and z. And I don't have your data, since all you gave us is a picture. It is just as easy to attach a .mat file, or even a text file! A picture of numbers is NOT worth a thousand words. It is almost worthless.
So I'll make up some data.
theta = (0:30:360)';
z = 0:.5:1.5;
r = rand(12,4)*3 + 20;
r(end+1,:) = r(1,:);
r
r =
22.128 22.254 22.443 22.752
22.264 20.765 20.731 20.858
20.828 21.518 22.788 22.272
22.039 22.097 21.05 22.261
21.965 22.673 20.59 21.141
20.488 22.878 20.753 21.703
20.357 21.642 21.848 20.228
21.495 20.416 21.42 20.162
22.879 20.448 21.055 21.592
21.021 20.773 22.492 22.338
21.756 22.522 21.756 22.802
20.671 20.763 21.649 20.39
22.128 22.254 22.443 22.752
The only thing of importance is I made sure the first and last radii are the same at 0 and 360 degrees. Note my use of sind and cosd, because you used degrees. Now just call surf.
surf(r.*cosd(theta),r.*sind(theta),ones(size(theta)).*z)
This will work as long as you have R2016b of MATLAb or later. Easy enough to fix using bsxfun or even repmat if you have an older release.
Would not even be that difficult to put an end cap on the ends. Just a pair of polygonal patches.

Muhamed Sewidan

### Muhamed Sewidan (view profile)

2019 年 3 月 22 日
yes it is a spreadsheet, ok thanks a lot.
One more thing is it possible to make this cylinder closed?
John D'Errico

### John D'Errico (view profile)

2019 年 3 月 22 日
Well, I did mention how to do that, but only briefly. You have the data for the lower and upper values of z in the cylinder. So you can create a polygonal region in x and y, with a fixed z. Then just call patch or fill3 with the coordinates of those points, and whatever color you wish to fill it in with. So one call per end of the cylinder.
Muhamed Sewidan

### Muhamed Sewidan (view profile)

2019 年 3 月 22 日
Thank you a lot, you saved me :D

サインイン to comment.

Translated by