how to use surf function?

10 ビュー (過去 30 日間)
Muazma Ali
Muazma Ali 2022 年 7 月 29 日
編集済み: John D'Errico 2022 年 7 月 29 日
I found on the internet that it is posssible to use surf instead of using gscatter, but I am wondering whether it can take in single column matrix as a the 'z' argument.
I am experiencing problems when I am converting my one column array to a single column matrix for so passing it to surf.

回答 (1 件)

John D'Errico
John D'Errico 2022 年 7 月 29 日
編集済み: John D'Errico 2022 年 7 月 29 日
Almost always, when someone says they have a single vector of z values and they want to use surf, they mean that they have a list of points, scattered in nature, that they believe falls on some surface. A surface here is intended to describe a 2-dimensional curved shape. And when these individuals say this, they want MATLAB to somehow know how to connect those points into a surface.
SORRY. Surf cannot do that. SURF expects an array of points. Think of it as if you generate a grid, using a tool like meshgrid in the (x,y) plane. Now evaluate some function z(x,y). That is what surf can plot. It needs to know how the points are connected together. If all you will give it are a set of points, but then give it no hint as to how things are connected together? Then SURF will fail you. That is called scattered data.
How can you solve it? Typically, one is forced to use a tool for interpolation that can handle scattered data (scattereInterpolant is a good choice here.) You pass a grid of points, created using meshgrid, through the 2-dimensional interpolation tool. That generated a nice regular gridded surface, and surf is now happy and able to work.
Another option is to use a tool like my gridfit, as found on the file exchange. It uses approxaimtion methods to try to find a surface that passes through the set of scattered points you provide. Again, the result, if your data is any good and not too noisy, will be a nice regular surface.
In both of the above cases, if your data happens to NOT follow a nice, regular, single valued relationship, then you need to use something else. For example, a set of points that lie on the surface of a sphere will cause these tools to fail, since they presume a single-valued relationship.

Community Treasure Hunt

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

Start Hunting!

Translated by