You are on page 1of 6

1/13/2018 How do I extract data from MATLAB figures?

- MATLAB Answers - MATLAB Central

Related Content

MathWorks Support

How do I extract data points from a plot?


5 Answers 
Follow Vote
8 MATLAB Answers

How do I extract data from MATLAB figures?  extracting data series from *.fig file
Asked by MathWorks Support Team  on 10 Jul 2013
3 Answers
Latest activity Answered by Felipe Bittencourt de Souza on 15 Dec 2017 at 1:10
Accepted Answer by MathWorks Support Team  I'm trying to get the data from a
3,480 views (last 30 days)
zoomed-in MATLAB figure. I was
I have a few MATLAB figures, but no MATLAB code associated with it. I want to extract the data from the curves in the able to get the XData and YData
figures. of the full field of view figur...

1 Answer

Graphic objects and XData and


YData

 5 Answers 1 Answer

Entire Website

Handle This!

Blogs
Vote
Pixel grid
18
Link Blogs
Answer by MathWorks Support Team  on 8 May 2015
 Accepted Answer MATLAB R2014b Graphics - Part
2: Using Graphics Objects

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

Tags
MATLAB Answers™
Assume that the figure is stored in a file called 'example.fig'.

1. Open the figure file: extract data figure

fig line
open('example.fig');

%or Products
figure;
plot(1:10) MATLAB

2. Get a handle to the current figure:


Discover what MATLAB®
h = gcf; %current figure handle
can do for your career.
Opportunities for recent
3. The data that is plotted is usually a 'child' of the Axes object. The axes objects are themselves engineering grads.
children of the figure. You can go down their hierarchy as follows:
 Apply Today
axesObjs = get(h, 'Children'); %axes handles
dataObjs = get(axesObjs, 'Children'); %handles to low-level graphics objects

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

objTypes = get(dataObjs, 'Type'); %type of low-level graphics object

• 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.

5. Lines of code similar to the following would be required to bring the data to MATLAB Workspace:

https://www.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures 1/6
1/13/2018 How do I extract data from MATLAB figures? - MATLAB Answers - MATLAB Central

xdata = get(dataObjs, 'XData'); %data from low-level grahics objects


ydata = get(dataObjs, 'YData');
zdata = get(dataObjs, 'ZData');

 6 Comments

Show 3 older comments


 Marianna Biscarini on 1 Dec 2017

Hi, thanks for the very useful suggestions.

I have some problem when the file .fig that I open contains more figures and each figure
is composed by several subplots.

How do I extract data from one precise subplot of one precise figure contained in the .fig
file?

 Michael Judge on 4 Dec 2017

Hello all,

Just wanted to point out that, if the data were plotted as a 2D matrix (where each row is a
different line), none of the solutions on this thread give back the initial matrix (after
conversion of YData to a matrix, of course). In fact, the rows in the extracted data are all
out of order.

Any thoughts on this? Are lines created/indexed in any particular order when they are
plotted from a matrix?

Thanks, Michael

 Walter Roberson  on 4 Dec 2017

When you plot a matrix by columns, then the order of handles returned from the plot() call
is the order of the columns:

data = sort(rand(20,5),2);
h = plot(data);

Now h(1) corresponds to column 1, h(2) corresponds to column 2, and so on. You can
confirm this with:

h(1).DisplayName = 'col1';
h(2).DisplayName = 'col2';
h(3).DisplayName = 'col3';
h(4).DisplayName = 'col4';
h(5).DisplayName = 'col5';
legend();

and see that indeed the item labeled col5 is the one with highest average Y (it was
constructed that way by the sort() call).

However, the order of axes children defaults to the reverse of this:

>> get(gca,'Children')
ans =
5×1 Line array:

Line (col5)
Line (col4)
Line (col3)
Line (col2)
Line (col1)

because the rule is that the axes children are (by default) painted from last to first (first is
on top, last is on bottom). This can be altered in a few ways, including some obscure
specialized settings that were new in R2014b, but also the order can be changed with
good old uistack()

https://www.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures 2/6
1/13/2018 How do I extract data from MATLAB figures? - MATLAB Answers - MATLAB Central
When you recall a figure file and pull out the axes children, the axes children are going to
be in the same order as was present in the axes when it was saved to the figure file. If
nothing in the original code altered the order, that is going to be last column first. So if you
retrieve the YData and mat2cell() it into a 2D matrix, make sure to fliplr() to get the
original order.

Log in to comment.

Vote
3
Link
Answer by Michael on 13 Jun 2014
Edited by Michael on 13 Jun 2014

I tried to follow these steps, but when I got to objTypes = get(dataObjs, 'Type') I got this error:

Error using get Conversion to double from cell is not possible.

I don't know Matlab's figure format and I'm not familiar with Matlab's API for accessing figure data.
I'm not sure what this error means.

If anyone else happens upon this: Matlab figures are just ".mat" files. The scipy.io library in Pylab
can read Matfiles into numpy structures using the 'loadmat' command. One can then browse the
figure data in Python and locate the data.

Edit: one can also step through the figure data in Matlab, by loading the figure using the command
"s=load('Figure.fig','-mat')". The solutions using "get" never really worked for me. I think this is
because every figure is structured slightly differently, and people are posting solutions that work for a
particular figure, but don't generalize well. If you just grab the figure data structure, you can step
through it and find what you need.

 3 Comments

 Cameron on 30 Jun 2014

I managed to get a similar error when running the line:

dataObjs = get(axesObjs, 'Children');

I'm not entirely sure why it's unable to access the handles using the 'get' method, but
changing the code to

dataObjs = axesObjs.Children;

seems to have done the trick. Hope this helps.

 Julia Mödl on 26 Jun 2015

i tried all answers and always get an error. i got the converting to double error too and
with the last answer i got this error: No appropriate method, property, or field 'Children' for
class 'matlab.graphics.Graphics'.

Error in daten_auslesen (line 5) dataObjs = axesObjs.Children; I dont know how to fix this
code, so id be happy to get some help.

 Walter Roberson  on 12 Nov 2016

Julia Mödl : you would have that problem if the axes you were looking at was a
placeholder rather than an axes object.

https://www.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures 3/6
1/13/2018 How do I extract data from MATLAB figures? - MATLAB Answers - MATLAB Central

all_axesObjs = findobj(h, 'type', 'axes');

now only examine the items in all_axesObjs.

Or use what I posted earlier,

lineObjs = findobj(dataObjs, 'type', 'line');


xdata = get(lineObjs, 'XData');
ydata = get(lineObjs, 'YData');

Log in to comment.

Vote
1
Link
Answer by Parrish.Ch on 23 Jul 2015

Hi all,

I noticed people were having issues with getting the following error when attempting to run:

objsTypes = get(dataObjs,'Types')
Error using get Conversion to double from cell is not possible.

I think I have a solution to the issue. For surface plots, I noticed that the children of an axes object
(so dataObjs in this case) may contain a subgroup that is a complex cell. You have to use cell2struct
to break this cell into it's basic pieces so you can extract the data. Here is the code for my solution:

h = gcf;
axes = get(h,'Children');
dataObjs = get(axes,'Children');
Props = cell2struct(dataObjs,'SurfaceProps',2);
SurfaceData = Props.SurfaceProp;
XData = SurfaceData(3,1).XData;
YData = SurfaceData(3,1).YData;
ZData = SurfaceData(2,1).ZData;

**Before you just copy paste this code, there are a few important things to know.

My variable dataObjs is a 2x1 cell. The first index in the cell is empty but the second index is a 3x1
Group. I have to convert this cell group to a structure that can then be used to access my data. From
there, I use cell2struct on the second index to accomplish this. The cell2struct generates a property
that is named in the second argument of the cell2struct command ('SurfaceProp' for me).
Props.SurfaceProp extracts the various "children" from the 3x1 Group in dataObjs. In my case, I
have three objects in Props.SurfaceProp: two light objects and one surface object. The surface
object contains the x, y, and z data. My surface object is the third index in the matrix generated by
Props.SurfaceProp, so I use SurfaceData(3,1).XData to access the XData handle that is in the third
index of the SurfaceData array.

I hope this helps!

 2 Comments

 Houghton on 12 Nov 2016

I tried your code. But I got an error.

Undefined function 'cell2struct' for input arguments of type


'matlab.graphics.primitive.world.Group'.

https://www.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures 4/6
1/13/2018 How do I extract data from MATLAB figures? - MATLAB Answers - MATLAB Central
- I typed

which cell2struct

- I got the answer

built-in (/Applications/MATLAB_R2014b.app/toolbox/matlab/datatypes/@cell/cell2struct)
% cell method >>

what should I do?

Thanks

 Walter Roberson  on 12 Nov 2016

Would you have some simple code that recreates this problem? My simple test does not
create that kind of group, but it could well be that a different call does.

Log in to comment.

Vote
0
Link
Answer by Daniel Ares on 19 Jun 2017

Hi to everybody,

i can´t run it, i get this error, with this code.

open('Force vs Time.fig');

h = gcf; %current figure handle

axesObjs = get(h, 'Children'); %axes handles

dataObjs = get(axesObjs, 'Children'); %handles to low-level graphics objects in axes

lineObjs = get(dataObjs, 'type', 'line'); %type of low-level graphics object

xdata = get(lineObjs, 'XData'); %data from low-level grahics objects

ydata = get(dataObjs, 'YData');

zdata = get(dataObjs, 'ZData');

Thanks

 1 Comment

 Jan Simon  on 19 Jun 2017

You forgot to mention the error you get. But please remove this message and post it as a
new question, because this is the section for answers. Thanks.

You will get an answer like:

lineObjs = get(dataObjs, 'type', 'line')

get() does not accept 3 inputs. Do you mean:

lineObjs = findobj(dataObjs, 'type', 'line')

https://www.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures 5/6
1/13/2018 How do I extract data from MATLAB figures? - MATLAB Answers - MATLAB Central
? Note: Please delete this message even if this solves your problem.

Log in to comment.

Vote
0
Link
Answer by Felipe Bittencourt de Souza on 15 Dec 2017 at 1:10

I was having the same error message mentioned before: "Error using get Conversion to double from
cell is not possible."

I solved this issue with Walter Roberson's answer, using the following code:

open('example.fig');

a = get(gca,'Children');

xdata = get(a, 'XData');


ydata = get(a, 'YData');
zdata = get(a, 'ZData');

 0 Comments

Log in to comment.

Log in to answer this question.

mathworks.com
© 1994-2018 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See mathworks.com/trademarks for a list of additional trademarks. Other
product or brand names may be trademarks or registered trademarks of their respective holders.

https://www.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures 6/6

You might also like