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.
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Annotations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!