Extracting data from Contour plots

129 次查看(过去 30 天)
I am trying to extract data from a contour plot. I managed to get all the information about the dataObjs using
open('colormapinhomo.fig');
h = gcf;
axesObjs = get(h, 'Children');
dataObjs = get(axesObjs, 'Children')
dataObjs{2}
Contour with properties:
LineColor: [0 0 0]
LineStyle: 'none'
LineWidth: 0.5000
Fill: 'on'
LevelList: [1×62 double]
XData: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40]
YData: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
ZData: [20×40 double]
but cant get the data itself. Any help ? And is this the only way to extract data, or can I somehow output a .dat file of the figure with the data whether it was a contour or a histogram or even a simple plot line ?
  2 个评论
Adam
Adam 2017-5-18
dataObjs{2}.XData
dataObjs{2}.YData
dataObjs{2}.ZData
is the data. Generally you should keep hold of the data when you plot because extracting data from a plot is silly when you can just use the original data. If you've lost the original data though then the above is the best you can get from the plot.
Adam Danz
Adam Danz 2020-1-23
Also see this file exchange function that extracts the coordinates of all contour lines and organizes them in a table by level.

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2017-5-18
If you want the contour (x,y) values, you have to ask for them:
figure(1)
[C,h] = contour( ... );
The (2xN) ‘C’ array will have the (x,y) values of the contour lines. The first column in each has the contour value as the first row and the number of (x,y) values defining the contour as the second row. (It requires some coding to extract all of them effectively.) Use the LevelList property to find the values of the contours (these will match the first row and first column of each contour) so you can then use find to locate the beginning of each (x,y) contour matrix.
See the documentation for contour for details. There is a link to ‘Contour Properties’ that lists all of them.
  1 个评论
Star Strider
Star Strider 2022-7-2
With respect to your Comment, the centroid is the mean of the ‘x’, ‘y’, (and ‘z’) coordinates, so just take the mean values of the vectors —
[X,Y,Z] = peaks;
figure
[C,h] = contour(X,Y,Z, [1 1]*2); % Find One Contour At Level = 2
Cix = find(C(1,:)==2);
for k = 1:numel(Cix)
ixr = Cix(k)+1 : Cix(k)+C(2,Cix(k));
Cx{k} = C(1,ixr);
Cy{k} = C(2,ixr);
Crtd(:,k) = [mean(Cx{k}); mean(Cy{k})];
end
Crtd % Centroid Calculated From ‘mean’ Values
Crtd = 2×2
-0.5006 0.5180 -0.5978 1.0134
hold on
for k = 1:numel(Cix)
% plot(Cx{k}, Cy{k})
plot(Crtd(1,k), Crtd(2,k), 'rx')
end
hold off
title('Centroid Calculated From ‘mean’ Values')
figure
hold on
for k = 1:numel(Cix)
ps = polyshape(Cx{k}, Cy{k});
[psCtrdx(:,k),psCtrdy(:,k)] = centroid(ps);
plot(ps)
plot(psCtrdx,psCtrdy, 'rx');
end
psCrtd = [psCtrdx; psCtrdy] % Centroid Calculated By The ‘centroid’ Function
psCrtd = 2×2
-0.4788 0.4495 -0.5980 1.1698
hold off
axis([-3 3 -3 3])
title('Centroid Calculated By The ‘centroid’ Function')
The mean values and the centroid values are approximately the same.
This is a bit more involved for more contours (and requires a loop for each contour nesting each of these for that reason) however the approach is the same.
.

请先登录,再进行评论。

更多回答(1 个)

Walter Nagy
Walter Nagy 2021-4-17
编辑:Walter Nagy 2021-4-17
Try this code.
[c2,h2] = contour(xi,yi,zi,...); % your contour plot
c_lev=h2.LevelList; % get all contour levels
ind=find(c2(1,:)==max(c_lev)-1); % find index to the contour level of interest (one below the max in this case)
n_points=c2(2,ind); % number of points along the contour
contour_points=[c2(1,ind+1:ind+n_points) ; c2(2,ind+1:ind+n_points)]; % x, y coordinates of contour points.
pgon=polyshape(contour_points(1,:),contour_points(2,:)); % plots contour shape
[ac, bc]=centroid(pgon); % finds centroid of contour
  1 个评论
Fereshteh
Fereshteh 2022-7-2
Hello
I need to find centroid of contour but I dont have "polyshape" in my matlab. Is it in new version of matlab?
How can I find this Function?
Thank you.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by