MATLAB Answers

How can I extract a line from a surface?

30 views (last 30 days)
Leonardo Colavitti
Leonardo Colavitti on 17 Feb 2017
Hello everybody, I would like to extract a line from a surface. I already represent quite easily with the commands plot3, mesh and surf my surface, that is divided in three parts because it represents three different domains. My initial input (in attachment the file InputValues.txt)is composed by a matrix (18011*3) of Longitude, Latitude and Depth values. I would like to build a function that in input takes the position of start (A) and end of my profile (B) (xa,ya,xb,yb) and in output return the position of the corresponding z in A and B {e.g. [A,B] = myfunc(xa,ya,xb,yb)}. Finally, I would like to plot the obtained curve after cutting my surface with a vertical plane. I have already achieved this result with another software (Global Mapping Tools, very used in geophysical applications), but now I'd like to obtain the same thing in Matlab environment, of which I am quite new. Someone is familiar with these kind of problems? Do you have any suggestions, please? Thanks in advance,
Leonardo

  0 Comments

Sign in to comment.

Answers (1)

Sindhuja Parimalarangan
Sindhuja Parimalarangan on 21 Feb 2017
It is possible to extract z-value for given x and y values using the find function. For example,
h = surf(x,y,z);
[xvalue,yvalue] = find((h.XData==xa) & (h.YData==ya));
Please consider floating point tolerance in the "==" comparison.
You can model your function based on the above code.
Regarding obtaining the curve after a vertically cutting the surface, you could use the contour3 function as described in this link.

  1 Comment

Leonardo Colavitti
Leonardo Colavitti on 21 Feb 2017
Hello Sindhuja, thanks for your answer but I think that my problem is a bit more complicated. First, i think that the function 'surf' works with matrices as input and in my case x, y and z are vectors. So, I used the function meshgrid that returns 2-D grid coordinates based on the coordinates contained in vectors x and y. Moreover, I wrote the equation of the line through 2 points, that are inside my domain. Startpoint has arbitrary coordinates x1,y1 and endpoint is x2, y2. For the z_grid instead I used the function griddata, that is used to interpolate scattered data. The process is a bit slow but at the end I was able to get a matrix along the vertical coordinate. My problem at this point is to find the z position for x and y coordinates belonging to the straight line. I tried to do this, with the interp2 function but I get the following error:
[Z]=interp2(x_grid, y_grid, z_grid, X, Y);
Error using griddedInterpolant The grid was created from grid vectors that were not strictly monotonic increasing.
Error in interp2>makegriddedinterp (line 228) F = griddedInterpolant(varargin{:});
Error in interp2 (line 136) F = makegriddedinterp(X, Y, V, method,extrap);
What am I doing wrong?
For details, in attachment, you can find the code.
Thanks again for your time and your availability, I really hope someone can help me,
Leonardo

Sign in to comment.