3D cylinder code debugging
    3 views (last 30 days)
  
       Show older comments
    
    Miles Hao Peng Su
 on 3 Dec 2022
  
    
    
    
    
    Commented: Miles Hao Peng Su
 on 4 Dec 2022
            Hi, I am trying to implement a 3D cylinder code, and here is the code:
nCS = 2; %number of cross section of cylinder
nNodes = 50; %number of nodes per cross section
rSC = size * ones(1, nNodes);       %set length of each vertices
theta = linspace(0, 2* pi, nNodes); %set angle of each vertices
polar(theta, rSC) 
zSC = linspace(0, hSC, nCS)';   %create z coordinates, with height hSC
[xSC,ySC] = pol2cart(theta, rSC);   %transform from polar to cartisen
XSC = repmat(xSC, nCS, 1);      %repeat for array of number of cross sections
YSC = repmat(ySC, nCS, 1);
ZSC = repmat(zSC, 1, nNodes);   %repeat for number of z-layers
xSC_lid = zeros(2, nNodes);     %creating the top and bottom lid
ySC_lid = zeros(2, nNodes);
zSC_lid = repmat([0, hSC], 1, nNodes);
X = [xSC_lid(1,:); XSC; xSC_lid(2,:)];
Y = [ySC_lid(1,:); YSC; ySC_lid(2,:)];
Z = [zSC_lid(1,:); ZSC; zSC_lid(2,:)]; 
surf(X, Y, Z)
The error message is 

What is wrong and how to solve this?
0 Comments
Accepted Answer
  Matt J
      
      
 on 3 Dec 2022
        
      Edited: Matt J
      
      
 on 3 Dec 2022
  
      nCS = 2; %number of cross section of cylinder
nNodes = 50; %number of nodes per cross section
[X,Y,Z]=cylinder(ones(1,nCS),nNodes);
surf(X,Y,Z,'FaceAlpha',0.7)
patch('XData',X(1,:),'YData',Y(1,:), 'ZData',Z(1,:),'FaceColor','r');
patch('XData',X(nCS,:),'YData',Y(nCS,:), 'ZData',Z(nCS,:),'FaceColor','r');
6 Comments
More Answers (1)
  Torsten
      
      
 on 3 Dec 2022
        zSC_lid is an 1 x nNodes vector:
zSC_lid = repmat([0, hSC], 1, nNodes);
Thus it has only one row.
But you reference the second row in the command
Z = [zSC_lid(1,:); ZSC; zSC_lid(2,:)];
which does not exist. 
Thus MATLAB throws an error.
See Also
Categories
				Find more on Logical 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!



