Extract x,y,z coordinates from figure

25 vues (au cours des 30 derniers jours)
Aidan Turner
Aidan Turner le 10 Mar 2021
Modifié(e) : Jorg Woehl le 11 Mar 2021
I have a figure which is a 3D surface plot. I am trying to create a scalar point map in a CSV file type. For this I need to extract the X,Y and Z components for each point in the grid. I would ultimately like the results to be formatted as below.
Any help is greatly appreciated.

Réponses (3)

Star Strider
Star Strider le 10 Mar 2021
If you used the ‘x’ and ‘y’ vectors from your previous post Add x and y Values to z data from excel file and if you want to save them along with the ‘z’ coordinates, it will first be necessary to create the vectors as matrices, using either ndgrid or meshgrid, then reshape them using the (:) subscript operator to create column vectrors from them.
x=linspace(0,100,20);
y=linspace(0,28,37);
z = (37x20);
[X,Y] = ndgrid(x,y);
MatrixForCSV = [X(:) Y(:) z(:)];
That will create all the vectors correctly. You can then write the matrix to a .csv file.

Adam Danz
Adam Danz le 10 Mar 2021
Modifié(e) : Adam Danz le 10 Mar 2021
Depending on how you plotted the 3D surface, you may already have those coordinates before the plot is produced. For example, x, y, and z are the grid coordinates in this surface,
surf(x,y,z)
If you do not have access to those coordinates, for example, in surf(Z), you can extract them from the surface object handle,
h = surf(___);
coordinates = [h.XData(:), h.YData(:), h.ZData(:)]; % mx3 matrix
and if you want the color data, h.CData(:).

Jorg Woehl
Jorg Woehl le 10 Mar 2021
Modifié(e) : Jorg Woehl le 11 Mar 2021
Hi Aidan, let's take the peaks surface plot as an example for a 3D surface plot. We create a table from the surface data and write that to an Excel file:
[X,Y,Z] = peaks(25);
s = surf(X,Y,Z);
T = table(s.XData(:), s.YData(:), s.ZData(:),...
'VariableNames', {'X coordinate', 'Y coordinate', 'Z coordinate'});
writetable(T, 'myfile.xlsx');
The x, y, and z data are 2D matrices, but using (:) converts them to vectors while maintaining their relative positions, which is what we need for the table and spreadsheet. The Excel file looks like this:
If you don't have the handle s to your surface, you can find it using
s = findobj('Type', 'Surface')
(You may get more than one result if you have multiple surface objects open in your workspace, but can get to the individual ones using s(1), s(2), and so on.) Hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by