MATLAB: Extract scattered data from fig

figfigurefindobjgraphxdataydata

I want to extract data from a lot of graph fig files. My data are consist of scattered points in a graph. I used the code below however there is nothing in the x and y variables. How to fix this?
clc
clear all
addpath(genpath('PIV TEST 2'))
for k = 2:21
h = openfig(sprintf('%d.fig',k));
h = findobj(gca,'Type','line');
x = get(h,sprintf( 'XData',k));
y = get(h, sprintf('YData',k));
end
Is there anything wrong in my coding?
a.png

Best Answer

Here is a cleaned-up version of your code that finds any and all objects in the specified figure that contain an "XData" property. It will work no matter how the plots were produced (line, scatter, etc).
I suggest storing the (x,y) values in a cell array.
x = cell(20,1);
y = x;
for k = 2:21
fh = openfig(sprintf('%d.fig',k));
h = findobj(fh,'-Property', 'XData'); %any object with property "XData" on figure fh
x{k} = get(h,'XData');
y{k} = get(h,'YData');
end
If more than one object was located, x and y will be a cell array containing one element for each object.