Question on surface plotting (beginner)

조회 수: 39 (최근 30일)
Young Chan Jung
Young Chan Jung 2024년 10월 23일 4:15
댓글: Young Chan Jung 2024년 10월 23일 5:21
Hello, I am a beginner, and I have a problem plotting surface topography.
I have x,y,z data of the surface from AFM scan.
Scan was in x-direction, height is in z-direction. This means that for one line of x,z contour, y stays the same. After one line scan of x,z is finished, the next line moves to another z. like z=0 ... z= 1 ... z=2 ...
So my simple code is as below:
A=dlmread('surface.txt');
x = A(:,1);
y = A(:,2);
z = A(:,3);
plot3(x, y, z)
When I use plot3, two problems occur.
1. It seems that when the next y-line is plotted, the end and start of the y value gives unwanted connection
2. Points do not connect in y-direction.
I understand this is how plotting works, so I tried to search for other methods like
[x,y]=meshgrid(x,y);
surf(z)
This gives me an error message saying z has to be an array, not scalar or vector... But, isn't z an array from a data file..?
What command should i use to plot the surface from a experimental data?
Thank you.

채택된 답변

KSSV
KSSV 2024년 10월 23일 4:27
편집: KSSV 2024년 10월 23일 4:31
T = readtable('surface.txt') ;
x = T.(1) ; y = T.(2) ; z = T.(3) ;
F = scatteredInterpolant(x,y,z) ;
dx = 0.1 ; dy = 0.1 ; % resolution can be changed to desired
[X,Y] = meshgrid(min(x):dx:max(x),min(y):dy:max(y)) ;
Z = F(X,Y) ;
surf(X,Y,Z)
shading interp
Using griddata
T = readtable('surface.txt') ;
x = T.(1) ; y = T.(2) ; z = T.(3) ;
dx = 0.1 ; dy = 0.1 ; % resolution can be changed to desired
[X,Y] = meshgrid(min(x):dx:max(x),min(y):dy:max(y)) ;
Z = griddata(x,y,z,X,Y) ;
surf(X,Y,Z)
shading interp
  댓글 수: 1
Young Chan Jung
Young Chan Jung 2024년 10월 23일 4:36
WOW!!! Thank you for your quick and very good answer!! I will work with this and see what happens. Thank you so much!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

William Rose
William Rose 2024년 10월 23일 5:06
A file of simulated AFM scan data is attached, with X,Y,Z in columns 1,2,3. I assume you will know how many x-values there are for each value of y. In this example, there are 24 x values for each value of y.
data=dlmread('AFMscan2.txt');
x=data(:,1); y=data(:,2); z=data(:,3);
Nx=24; % number of x values for each y value
X=reshape(x,Nx,[]);
Y=reshape(y,Nx,[]);
Z=reshape(z,Nx,[]);
Method 1: Line plot for each y-value.
[nx,ny]=size(X);
figure
hold on
for j=1:ny
plot3(X(:,j),Y(:,j),Z(:,j),'-b')
end
xlabel('X'); ylabel('Y'); zlabel('Z'); grid on; hold off; view(45,35)
Method 2: Make a surface plot.
figure
surf(X,Y,Z,'EdgeColor','None')
xlabel('X'); ylabel('Y'); zlabel('Z'); grid on; view(45,35)
OK

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by