How can I extract data (CData) from a figure

41 views (last 30 days)
Hello,
I want to extract data from a .fig.
In reality I want the XData, YData and CData
Thank you in advance.

Answers (1)

Adam Danz
Adam Danz on 14 Jul 2023
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 Comments
Ricardo Duarte
Ricardo Duarte on 14 Jul 2023
Solved.
I just did:
imageobj = imhandles(gca)
cdata = imageobj.CData;
Thank you for your help.
Walter Roberson
Walter Roberson on 14 Jul 2023
%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.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!