Clear Filters
Clear Filters

plot contour line of three variable ?

1 view (last 30 days)
Akim Mahmud
Akim Mahmud on 20 Dec 2017
Hi I have thistext file ( please see attached )with 3 columns ( lat, lon and depth ) I want to plot a contour map of the water depth as a function of lat and lon. I used the following code contour (lat,lon,depth) where I extracted lat, lon, and depth separately but I get this error "Z must be at least a 2x2 matrix."
any help ? thanks

Answers (2)

Star Strider
Star Strider on 20 Dec 2017
Edited: Star Strider on 20 Dec 2017
If the file is very large, probably the easiest way to see if your data are gridded (such that the ‘x’ and ‘y’ values repeat in a specific, regular pattern) is to plot your vectors using the stem3 function. If they appear to be regularly-spaced, you can use the reshape function to form them into matrices. Then you can use your matrices with contour.
If they are not gridded, you will have to interpolate them to be gridded matrices. See the griddata (link) function and its friends to do that.
EDIT
If ‘M’ is your (Nx3) matrix:
x = linspace(min(M(:,1)), max(M(:,1)), size(M,1)+1);
y = linspace(min(M(:,2)), max(M(:,2)), size(M,1)+1);
[X,Y] = meshgrid(x, y);
Z = griddata(M(:,1), M(:,2), M(:,3), X, Y);
figure(1)
contourf(X, Y, Z)
axis equal
grid
Experiment to get the result you want.
  1 Comment
Akim Mahmud
Akim Mahmud on 20 Dec 2017
I am not sure, if I follow you correctly. I am still new to matlab. I have attached a sample txt from the big txt file. I have used the stem2 function to see the data I am not sure how to use reshape function to from them into matrices. Any help would be appreciated

Sign in to comment.


Harshana Rajakaruna
Harshana Rajakaruna on 15 Mar 2018
Thanks

Categories

Find more on Contour Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!