MATLAB Answers

How to contour this use matlab?

21 views (last 30 days)

Answers (1)

John D'Errico
John D'Errico on 8 Jan 2021
Are you asking how to write the code to take scattered data, and derive contours from pairs of points, using some variation of the scheme you show? If so, sorry, but nobody is going to write that complex code for you, since it is much more than just doing some simple linear interpolations. Then you need to connect those contours up smoothly.
Anyway, the entire scheme is not that good of an approach to solve the problem. So instead of using a kluge of an approach like this, you are better off using one of multiple tools that already exist in MATLAB. Essentially, you can do the contouring using one line of code, or at most about 3 lines, IF you use one of the existing tools.
The simplest solution is to just download one of at least two tools for contouring of scattered data from the file exchange. These are called tricontour. But those tools will perform the entire contouring process for you, starting only from scattered data.
Next, you can use a tool like scatteredInterpolant to interpolate your data onto a regular grid of points in the (x,y) plane. scatteredInterpolant is part of MATLAB itself. Everyone has it. MATLAB also has the griddata tool, which is an older version of the same idea. Then just use contour to produce a contour plot. Essentially, these tools will do the job in only about 3 lines of code, and do it simply with no real though or effort on your part.
Finally, you can download and use my own gridfit tool (again from the file exchange for free download). Again here, it produces a gridded surface on a regular lattice. Then you just call contour on that gridded surface.
But all of these methods are trivial to implement, and have better properties than does the ad hoc scheme you seem to want to use. For example, you can use these other tools to create a nice smooth surface, as opposed to the rather linear result you get implicitly from the linear interpolant you want to use.
The other virtue of use of such tools is you can create the surface itself to be viewed. for example, if you look at the gridfit link I gave you, you will see a surface that I re-created from scattered points using my gridfit tool.
The data came from a topographic map of an area near my home at the time.
So rather than try to write an ad hoc scheme, use one of the existing tools already available to you in MATLAB.


Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by