How can I extract data (CData) from a figure

41 次查看(过去 30 天)
Hello,
I want to extract data from a .fig.
In reality I want the XData, YData and CData
Thank you in advance.

回答(1 个)

Adam Danz
Adam Danz 2023-7-14
XData, YData, and CData are properties of (some) graphics objects. These graphics objects are within axes which are within a figure. A figure can have multiple axes and axes can have multiple graphics objects.
If you have the handle to a graphics object with those properties, you can get the property values using
h = surf(rand(20));
xdata = h.XData;
ydata = h.YData;
cdata = h.CData;
or
allValues = get(h,{'XData','YData','CData'});
  3 个评论
Ricardo Duarte
Ricardo Duarte 2023-7-14
Solved.
I just did:
imageobj = imhandles(gca)
cdata = imageobj.CData;
Thank you for your help.
Walter Roberson
Walter Roberson 2023-7-14
%every object that has XData also has YData
objs_with_xdata = findobj(h, '-property', 'XData');
xdata_cell = get(objs_with_xdata, 'XData');
ydata_cell = get(objs_with_xdata, 'YData');
%but some objects with XData and YData do not have ZData. For example
%image objects do not have ZData
zdata_cell = cell(size(xdata_cell));
for k = 1 : length(objs_with_xdata)
try
zdata_cell{k} = get(objs_with_xdata(k), 'ZData');
catch ME
zdata_cell{k} = [];
end
end
At the end of this, xdata_cell and ydata_cell and zdata_cell will be cell arrays, with one entry for each (non-hidden) object in the figure that has XData. For example if the figure contained an axes with one line, one filled (patch) area, a legend, and an image, then there would be entries for the line, and for the patch, and for the image. The ZData for 2D lines and for 2D patch() objects such as fill() is naturally []; the image() has no ZData property so the code would detect that and drop in [] instead.
The order of graphics objects within the cell arrays is variable; you should probably not count on the order being consistent.

请先登录,再进行评论。

类别

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

产品

Community Treasure Hunt

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

Start Hunting!

Translated by