How to Extract Data from a MATLAB Figure (.fig)

Retrieve Raw X, Y, Z Coordinate Data & Matrix CData from Saved MATLAB Plots

Direct Answer (TL;DR): To extract raw data from a .fig file in MATLAB, open the figure invisibly with fig = openfig('myplot.fig', 'reuse', 'invisible');, get the handle of the line or surface object using h = findobj(fig, 'Type', 'line');, and fetch data coordinates via x = get(h, 'XData'); y = get(h, 'YData');.
Extract Data from MATLAB Figure
4.9/5
Student Rating
500+
PhD Experts
100%
Confidential
15k+
Projects Delivered

Step 1: Extract Line Plot Coordinates (X, Y, Z)

If you lost your original source dataset but saved the MATLAB plot figure (.fig), follow this exact script to extract all data points:

% Step 1: Open figure invisibly in memory
figHandle = openfig('sample_plot.fig', 'reuse', 'invisible');

% Step 2: Find all line objects in current axes
lineHandles = findobj(figHandle, 'Type', 'line');

% Step 3: Extract X, Y, and Z data arrays
xData = get(lineHandles(1), 'XData');
yData = get(lineHandles(1), 'YData');

% Step 4: Close figure handle
close(figHandle);

% Output dataset dimensions
fprintf('Extracted %d data points successfully.\n', length(xData));

Step 2: Extract Data from Multi-Subplot Figures

When a .fig file contains multiple subplots, iterate through every axis handle to extract distinct datasets:

fig = openfig('multi_subplot.fig', 'reuse', 'invisible');

% Retrieve all axis handles
allAxes = findobj(fig, 'Type', 'axes');

extractedData = struct();

for k = 1:length(allAxes)
    currentLine = findobj(allAxes(k), 'Type', 'line');
    if ~isempty(currentLine)
        extractedData(k).X = get(currentLine(1), 'XData');
        extractedData(k).Y = get(currentLine(1), 'YData');
        fprintf('Subplot %d: Extracted %d points.\n', k, length(extractedData(k).X));
    end
end

close(fig);

Step 3: Extract Matrix Data from Images, Heatmaps & Contour Plots

To extract numerical matrix data from heatmaps, images, or contour figures, extract the CData property:

fig = openfig('heatmap_plot.fig', 'reuse', 'invisible');

% Locate image or surface handle
imgObj = findobj(fig, 'Type', 'image');
if isempty(imgObj)
    imgObj = findobj(fig, 'Type', 'surface');
end

% Extract raw matrix array
matrixData = get(imgObj(1), 'CData');
close(fig);

disp('Extracted Matrix Size:');
disp(size(matrixData));

Step 4: Export Extracted MATLAB Data (CSV, Excel, MAT-file)

Save your extracted arrays to `.csv`, `.xlsx`, or `.mat` formats for downstream work:

% Option A: Export to CSV / Excel
dataTable = table(xData', yData', 'VariableNames', {'X_Values', 'Y_Values'});
writetable(dataTable, 'ExtractedFigureData.csv');

% Option B: Save directly to MAT-file
save('ExtractedDataset.mat', 'xData', 'yData', 'matrixData');

Struggling with MATLAB Data Analysis?

Our PhD-qualified MATLAB developers can automate data extraction, signal processing, and model fitting for your academic or research project.

Get Instant Code Assistance

Frequently Asked Questions (AEO Direct Answers)

For 3D surface or mesh plots, set h = findobj(fig, 'Type', 'surface'); and retrieve zData = get(h, 'ZData'); alongside XData and YData.

findobj(fig, 'Type', 'line') returns an array of handles. Iterate through each line using a for loop: for i = 1:length(h), x{i} = get(h(i), 'XData'); end.
Quality Guarantee

Our Engineering Academic Assistance Standards

Plagiarism-free, custom-written technical reports and source files.

Extensive inline documentation and comments explaining concepts.

Worked derivations and mathematical proofs detailing calculations.

Unlimited revisions to ensure complete alignment with assignment rubrics.