# Drawing a segment of a cylinder from a matrix

6 views (last 30 days)

Show older comments

Yvotte Brits
on 6 Sep 2017

Commented: Yvotte Brits
on 14 Sep 2017

Hi, I want to do a 3d plot of a 1/9th slice of a cylinder of the attached data points in Matlab. There is 51 layers in the z-direction, 36 points in the r direction and 9 points in the theta direction. Every coordinate have a value in matrix form that have to be indicated as a color in the 1/9th slice of the cylinder. What is the best way to draw this matrix in a 3d plot in Matlab?

Regards

Yvotte

##### 1 Comment

Stephen23
on 13 Sep 2017

Edited: Stephen23
on 13 Sep 2017

### Accepted Answer

Jocie Kluger
on 14 Sep 2017

Edited: Jocie Kluger
on 14 Sep 2017

The general idea is to reshape the z, r, and theta coordinates into 2D matrices of size (53*37) x 9 that correspond to the color data. Then, transform the r and theta coordinates into Cartesian coordinates. Transform all of the matrices into vectors so that they can be plotted with the scatter3 command, where you can specify each marker color. Please refer to the documentation for details on the command syntax.

%Read and format data

filename = '3d Fluxes.xls';

sheet= 1;

z= xlsread(filename,sheet,'A:A'); %get z data

z(isnan(z))= []; %remove NaN

r= xlsread(filename,sheet,'B:B'); %Get r data

r(isnan(r))= [];

theta= xlsread(filename,sheet,'C2:K2'); %Get theta data

ColorData= xlsread(filename,sheet,'C1:K2067');

%Remove extraneous rows. Not most efficient way

removeRowInds= 1; %first row to remove. Excel merge in row 2 affects this.

for i= 1:52

removeRowInds= [removeRowInds 39*i 1+39*i];

end

ColorData(removeRowInds,:)= [];

ColorVector= reshape(ColorData, numel(ColorData), 1);

%%Reformat data so all 1D vectors.

%Order to take data: 37*53 rows. Then reshape column-wise.

%zVector should repeat r times theta times then change value

zRow= z'; %turn z into row vector

zMat1= repmat(zRow, 37, 1);

zVector1= reshape(zMat1, numel(zMat1),1);

zMat= repmat(zVector1, 1,9); %z value for each ColorData value

zVector= reshape(zMat,numel(zMat), 1); %turn z back into a vector

rMat= repmat(r, 1, length(theta));

rVector= reshape(rMat, numel(rMat), 1);

thetaMat= repmat(theta, 37*53,1);

thetaVector= reshape(thetaMat, numel(thetaMat),1);

%Turn r and theta into matrices of X and Y

X= rVector.*cos(thetaVector);

Y= rVector.*sin(thetaVector);

%%Final plot

S = repmat(25,numel(X),1); %Specify marker size

scatter3(X,Y,zVector, S, ColorVector);% scatter3(X,Y,Z,S,C)

### More Answers (0)

### See Also

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!