Clear Filters
Clear Filters

Cylinder generation by using a 3D matrix

2 views (last 30 days)
youssef mourad
youssef mourad on 2 Mar 2016
Answered: BhaTTa on 23 Jul 2024
Hello, I have created a circle on matlab then read image data as a 20*20 logical array. I then converted the 2D array into a 3D (Huge data set) ,Thus i have the 3D called B Which is (20*20*10) data matrix . I want to draw that 3D matrix data into a cylinder, how can i do so ? Thank you in advance

Answers (1)

BhaTTa
BhaTTa on 23 Jul 2024
To visualize a 3D data matrix as a cylinder in MATLAB, you can use the surf or mesh functions to plot the data on the surface of a cylinder. Here’s a step-by-step guide to achieve this:
  1. Create the Cylinder: Use the cylinder function to create the coordinates of the cylinder.
  2. Map the Data onto the Cylinder: Map your 3D data matrix onto the surface of the cylinder.
Here’s an example code to help you get started:
% Assuming B is your 3D data matrix of size 20x20x10
B = randi([0, 1], 20, 20, 10); % Example data, replace with your actual data
% Create a cylinder
radius = 1;
height = 1;
num_points = 20; % Number of points around the circumference
[X, Y, Z] = cylinder(radius, num_points - 1);
% Scale the Z-axis to match the height of your data matrix
Z = Z * (size(B, 3) - 1);
% Initialize the CData for the surface
C = zeros(size(X, 1), size(X, 2), size(B, 3));
% Reshape and interpolate the data to map onto the cylinder
for k = 1:size(B, 3)
% Interpolate the data to match the cylinder coordinates
[Xq, Yq] = meshgrid(linspace(1, size(B, 2), size(X, 2)), linspace(1, size(B, 1), size(X, 1)));
C(:, :, k) = interp2(B(:, :, k), Xq, Yq, 'linear');
end
% Plot the cylinder with the data mapped onto it
figure;
hold on;
for k = 1:size(B, 3)
surf(X, Y, Z + (k - 1), C(:, :, k), 'EdgeColor', 'none');
end
hold off;
% Set the view and axis properties
axis equal;
view(3);
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('3D Data Mapped onto a Cylinder');
colorbar;

Community Treasure Hunt

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

Start Hunting!