How do I extract data from MATLAB figures?

Illustration
Michaelsmith94 - 2020-04-03T16:31:33+00:00
Question: How do I extract data from MATLAB figures?

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.

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-12-15

 

Yes, you can extract the data from curves in MATLAB figures (.fig files) even without the original code! Since .fig files store the plotted data internally, MATLAB provides easy ways to access it programmatically. I'll cover two main methods: direct extraction (if you have MATLAB access) and digitization tools (if you only have images of the figures).

 Direct Extraction from .fig Files 

This pulls the exact X and Y data from the curves without any approximation.

  1. Open the Figure:

    matlab
     
    openfig('your_figure.fig');  % Replace with your file name
     
     
  2. Get the Axes and Line Handles:

    matlab
     
    ax = gca;  % Get current axes
    lines = findobj(ax, 'Type', 'line');  % Find all curve lines
     
     
  3. Extract Data:

    matlab
     
    for i = 1:length(lines)
        x_data = get(lines(i), 'XData');  % X values for curve i
        y_data = get(lines(i), 'YData');  % Y values for curve i
        
        % Display or save
        disp(['Curve ' num2str(i) ':']);
        disp([x_data(:), y_data(:)]);  % Shows as [X Y] matrix
        
        % Optionally save to CSV
        writematrix([x_data(:), y_data(:)], ['curve_' num2str(i) '.csv']);
    end
     
     
    • If there are multiple curves, this loops through them.
    • For subplots or other elements (e.g., error bars), use findobj with more filters like 'Tag' if set.
    • No need to display the figure—run this on a closed file too!

    Example output: A matrix of [X Y] pairs you can plot, analyze, or export.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!