Draw 3D plot from 2D plot with discrete data points

Hi All,
I am trying to draw 3D plot from a 2D line graph (in x/z-axis).
I have discrete data points in x and z-axis like below graph. I want to roate it around z-axis. Therefore, I used 'surf'
Here is what I obtained so far.
filename = 'filename.xlsx';
x(:,:)= xlsread(filename);
r = (x(4:49,12))'; z = (x(4:49,13))'; %% data from excel
for i = 1:46;
theta = linspace(0,2*pi,200); %% to rotate around z-axis
X = r(i)*cos(theta);
Y = r(i)*sin(theta);
Z = z(i)*ones(length(Y), length(X));
plot(X,Y); %% just for checking X and Y are on circles
surf(X,Y,Z); %% wanted to obtain a smooth mountain with an empty center.
if i < 46;
hold on;
i = i+1;
end;
end
xlabel('x'); ylabel('y');zlabel('z');
hold off
shading interp
colorbar
and this is the result..
The profile has to be very smooth, no sharp edges on the boundary. However, it has four edges. Also, when x<450, there is be no data points in z-axis.. but it shows very flat top surface (yellow). I think I need to assign z-values at the correct x and y coordinates. However, I am not sure how to fix this.
Please help me to resolve this issue.
Thanks a lot for your comments and time in advance!

 Respuesta aceptada

Without the Excel file, I experimented to create an approsimately equal curve. I then used it as the radiuc argument to the cylinder fucntion to create the surf plot.
x = linspace(460, 700, 50);
z = 1E-194*exp(-0.025*x+460) + 1.5;
figure
plot(x, z)
title('Radius Curve')
[X,Y,Z] = cylinder(z,50);
X = X .* x(:)/2;
Y = Y .* x(:)/2;
Z = Z*max(z) + min(z);
figure
surfc(X, Y, Z, 'EdgeColor','none')
colorbar
zlim([0 max(z)])
That likely approximates what appears to have been requested. Make appropriate changes so it works correctly with the actual data.
.

8 comentarios

autumn
autumn el 20 de Jul. de 2021
Thanks a lot! I struggled with resolving this all day, and finally I obtained what I wanted. Thanks again!
As always, my pleasure!
.
If you don't mind, I have one follow-up question. Using your code, I tried this
figure
plot(x,z,'r', 'Linewidth',1.5); hold on
plot(X,Z);
xlabel('x'); ylabel('z');
and this is what I received. I expect that 'the red line (originally given profile)' will be overlapped with the outmost line among multiple lines. So I modified the code, only 'Z' like below.
Z = Z*(max(z)-min(z)) + min(z)
Now z-values look OKAY but not sure what's happening in x-axis. Cylinder function seems like shrinking the given profile data, but I could not find a certain ratio in this case. Can you help me to understand what's going on here?
The ‘z’ vector is the original profile, and you are also plotting it. (I did not have the original ‘z’, so I created one for the demonstration plot.)
The only possibility that comes to mind is that you need to change my code so that the number of elements in the ‘z’ you are using is the same number as used to create the cylinder result. Note that in my code, that parameter and the ‘z’ I used, matched (50). (This is one reason that providing the actual data is so important. I would have yoused your ‘z’ if I had it to work with.)
.
Thanks for the prompt reply.
I understand what you meant. But I didn't use my data to creat the two figures right above. I used the example code you provided.
I think the red line has to be the outmost of the multiple lines because Cylinder function is to rotate the red line around z-axis if my understanding is correct. But the result showed that after Cylinder function, the data points are expanded overall.
x = linspace(460, 700, 50);
z = 1E-194*exp(-0.025*x+460) + 1.5;
[X,Y,Z] = cylinder(z,50);
X = X .* x(:)/2;
Y = Y .* x(:)/2;
Z = Z*max(z) + min(z);
figure
plot(x, z,'r','Linewidth',1.5); hold on
plot(X, Z);
xlabel('x'); ylabel('z'); hold off
legend ('cylinder function', 'original data');
figure
surfc(X,Y,Z,'EdgeColor','none');
xlabel('x');ylabel('y');zlabel('z');
this is the result of the code.
Actually, the red ‘z’ line is essentially irrelevant. It simply creates the radius argument for the cylinder call, and has no use beyond that. There is no reason to plot it along with the cylinder surf plot.
autumn
autumn el 23 de Jul. de 2021
Thanks for the reply. It helped me a lot to understand it. :)
As always, my pleasure!
.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 19 de Jul. de 2021

Comentada:

el 23 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by