I have a few MATLAB figures, but no MATLAB code associated with it. I want to extract the data from the curves in the figures.
Kshitij Singh answered .
2025-11-20
You can extract data from MATLAB figures even if you don't have the original MATLAB code. Here’s a step-by-step guide to help you:
Open the Figure: If the figure is saved as a .fig file, you can open it using the openfig function. If the figure is already open, you can use gcf to get the current figure handle.
Access the Axes and Data Objects: Use the Children property of the figure to access the axes, and then access the data objects within the axes.
Extract Data: Retrieve 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 will extract the XData and YData from the first plotted line in the figure. If there are multiple lines or other types of data objects, you can loop through dataObjs to extract data from each one.