How to combine three arrays containing x and y values and a magnitude, respectively, in order to produce contour plot?
3 views (last 30 days)
Show older comments
Charis Horn
on 16 Jun 2018
Commented: Charis Horn
on 18 Jun 2018
Hi all,
I'm trying to create a filled contour plot in order to represent my data. However, functions like contourf require the input to be a matrix of values. I don't have a matrix of values, instead I have three arrays: one containing x-coordinates, one containing y-coordinates and a third array containing the magnitudes by which I want each coordinate point to be contoured. Is there any way to combine these three arrays into a matrix that can be read by a function such as contourf, or is there another function I can use which produces the same result but can read arrays instead? I've tried meshgrid and nfgrid but these just replicate the values in my arrays, which is not the result I want.
Thanks.
0 Comments
Accepted Answer
Walter Roberson
on 16 Jun 2018
Nx = 129; Ny = 129; %output graph should be Ny pixels high and Nx pixels wide
NL = 10; %number of levels to use in output contour
xv = linspace(min(x), max(x), Nx);
yv = linspace(min(y), max(y), Ny);
[xq, yq] = ndgrid(xv, yv);
maggrid = griddata(x(:), y(:), magnitude(:), xq, yq);
contourf(xq, yq, maggrid, NL);
You can also replace NL with a vector of specific levels to draw contour lines at.
More Answers (0)
See Also
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!