I would like to extract data points from a plot.
Prashant Kumar answered .
2025-11-20
You can extract data points from a plot in MATLAB even if you don't have access to the original data. Here are the steps to do this:
Open the Figure: If the figure is saved as a .fig file, use the openfig function. If the figure is already open, use gcf to get the current figure handle.
Access the Plot Data: Use the Children property to access the axes and data objects.
Retrieve the Data: Extract the XData and YData properties from the data objects.
Here’s an example:
% Open the figure file (replace 'your_figure.fig' with your actual file name)
fig = openfig('your_figure.fig');
% Alternatively, if the figure is already open
% fig = gcf;
% Get the axes objects
axObjs = fig.Children;
% Get the data objects within the axes
dataObjs = axObjs.Children;
% Extract XData and YData from the first data object (assuming it's a Line object)
x = dataObjs(1).XData;
y = dataObjs(1).YData;
% Display the extracted data
disp('X Data:');
disp(x);
disp('Y Data:');
disp(y);
This code extracts the XData and YData from the first plotted line in the figure. If there are multiple lines or data objects, you can loop through dataObjs to extract data from each one.