Friday, June 21, 2013

How do I extract data from MATLAB figures?

Subject:

How do I extract data from MATLAB figures?

Problem Description:

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.

Solution:

Below is a step by step example to extract data from the curve in a MATLAB figure :

Assume that the figure is stored in a file called 'example.fig'.

<step 1>
Open the figure file:

open('example.fig');

%or
figure;
plot(1:10)


<step 2>
Get a handle to the current figure:

h = gcf;


<step 3>
The data that is plotted is usually a 'child' of the Axes object. The axes objects are themselves children of the figure. You can go down their hierarchy as follows:

axesObjs = get(h, 'Children');
dataObjs = get(axesObjs, 'Children'); 


<step 4>
Extract values from the dataObjs of your choice. You can check their type by typing:

objTypes = get(dataObjs, 'Type');


* NOTE : Different objects like 'Line' and 'Surface' will store data differently. Based on the 'Type', you can search the documentation for how each type stores its data.

<step 5>
Lines of code similar to the following would be required to bring the data to MATLAB Workspace:
xdata = get(dataObjs, 'XData');
ydata = get(dataObjs, 'YData');
zdata = get(dataObjs, 'ZData');

No comments:

Post a Comment