Extract data points from multiple plot in one figure on Matlab

23 views (last 30 days)
I'm trying to figure out how to extract data points from a figure that has 3 lines plotted on the same graph, and I have no idea how to approach this. I need (x,y) data of each line. Can anyone help me? I have attached my code and figure.
openfig('multiple plots.fig')
h = gcf;
axesObjs = get(h, 'Children');
dataObjs = get(axesObjs, 'Children');
objTypes = get(dataObjs, 'Type');
xdata = get(dataObjs, 'XData');
ydata = get(dataObjs, 'YData');
% extract data
for i = 1:numel(xdata)
fprintf('%d line data\n',i) ;
xdata{i}
ydata{i}
end

Answers (1)

MarKf
MarKf on 1 Feb 2023
Your figure axes have 3 graphic objects, the first is the green line and the last is the Observed and Simulation ones. In between is the legend. It's the way the figure was created. So in theory you could extract the data programmatically like this:
openfig('multiple plots.fig');
h = gcf;
axesObjs = get(h, 'Children');
dataObjs = get(axesObjs, 'Children');
for i = 1:numel(dataObjs)
objTypes{i} = get(dataObjs{i}, 'Type');
if strcmp(objTypes{i},'line')
xdata{i} = get(dataObjs{i}, 'XData');
ydata{i} = get(dataObjs{i}, 'YData');
end
end
then you end up with data in x/ydata{1} and x/ydata{3}{[1,2]} (x/ydata{2} empty).
  1 Comment
Javed Ahmad
Javed Ahmad on 2 Feb 2023
Thanks for the help Marco Fuscà. Now I am able to extract data points of the two curve (blue and red colour) perfectly. But data points of linear line (green colour) gives a plot which is a scale-up value of what is shown in figure.
Can you help me on this?
I am attaching excel sheet of extracted data points and both the 'charts' for your reference. Chart 1 with all 3 lines and Chart 2 with only curve plots.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!