Question on surface plotting (beginner)

6 views (last 30 days)
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.

Accepted Answer

KSSV
KSSV on 23 Oct 2024
Edited: KSSV on 23 Oct 2024
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 Comment
Young Chan Jung
Young Chan Jung on 23 Oct 2024
WOW!!! Thank you for your quick and very good answer!! I will work with this and see what happens. Thank you so much!

Sign in to comment.

More Answers (1)

William Rose
William Rose on 23 Oct 2024
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

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!