How Do I Extract Data From MATLAB Figures - MATLAB Answers - MATLAB Central
How Do I Extract Data From MATLAB Figures - MATLAB Answers - MATLAB Central
Related Content
MathWorks Support
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
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'.
fig line
open('example.fig');
%or Products
figure;
plot(1:10) MATLAB
4. Extract values from the dataObjs of your choice. You can check their type by typing:
• 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
6 Comments
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?
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
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).
>> 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:
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
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;
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.
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
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.
2 Comments
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
built-in (/Applications/MATLAB_R2014b.app/toolbox/matlab/datatypes/@cell/cell2struct)
% cell method >>
Thanks
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,
open('Force vs Time.fig');
Thanks
1 Comment
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.
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');
0 Comments
Log in to comment.
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