How can I extract data (CData) from a figure

42 visualizaciones (últimos 30 días)
Ricardo Duarte
Ricardo Duarte el 14 de Jul. de 2023
Comentada: Walter Roberson el 14 de Jul. de 2023
Hello,
I want to extract data from a .fig.
In reality I want the XData, YData and CData
Thank you in advance.

Respuestas (1)

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

Iniciar sesión para comentar.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by