Plotting potential as a function of x and y coordinates

I have a .dat file that contains x and y position and the third column is potential for the corresponding points. How can I plot this data in Matlab (as a heat map or otherwise)? I tried using contour and contourf but I had some issues.

 Accepted Answer

Try one of these:
M = textread('potential.dat');
x = M(:,1);
y = M(:,2);
z = M(:,3);
xv = linspace(min(x), max(x), 25); % Create Vector
yv = linspace(min(y), max(y), 25); % Create Vector
[Xm,Ym] = ndgrid(xv, yv); % Create Matrices
Zm = griddata(x,y,z,Xm,Ym); % Interpolate
figure
surf(Xm, Ym, Zm)
grid on
view(0,90)
figure
heatmap(Zm)
figure
contourf(Xm, Ym, Zm)
I used textread because I had no idea what it was or what the format was, since .dat files can be anything.
The controuf plot:
.

4 Comments

That's great, thanks! How can I generate a key with this plot?
As always, my pleasure!
I am not sure what ‘key’ means in this context. Please describe what you would like.
Sorry, I mean a colour bar to show how the colours represent the potentials but I found a way to do this with the figure open. Is there a way to get the colour bar to automatically show when the figure is generated?
No worries!
Add a colorbar call:
figure
contourf(Xm, Ym, Zm)
colorbar
also:
axis('equal')
after the contourf call (or other calls) might improve its appearance.
.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!