How Do I Extract Data From MATLAB Figures_ - MATLAB Answers - MATLAB Central

  • Upload
    mjasd

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

  • 7/26/2019 How Do I Extract Data From MATLAB Figures_ - MATLAB Answers - MATLAB Central

    1/7

    4/12/2016 How do I extract data from MATLAB figures? - MATLAB Answers - MATLAB Central

    http://w ww.m athw or ks.com /m atl abcentr al /answ er s/100687- how -do- i- extr act- data- fr om -m atl ab- fi gur es

    Vote

    4

    Flag

    Asked by MathWorks Support Team on 10 Jul 2013

    Latest activityAnswered by Parrish.Ch on 23 Jul 2015

    Accepted Answer by MathWorks Support Team

    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.

    0 Comments

    How do I extract data from MATLAB figures?

    extract data figure fig line

    Tags

    MATLAB

    Products

    Related Content

    http://www.mathworks.com/matlabcentral/answers/?term=tag%3A%22extract%22http://www.mathworks.com/matlabcentral/answers/?term=tag%3A%22data%22http://www.mathworks.com/matlabcentral/answers/?term=tag%3A%22extract%22http://www.mathworks.com/matlabcentral/profile/authors/4622813-mathworks-support-teamhttp://www.mathworks.com/matlabcentral/answers/?term=product%3A%22MATLAB%22http://www.mathworks.com/matlabcentral/answers/?term=tag%3A%22line%22http://www.mathworks.com/matlabcentral/answers/?term=tag%3A%22fig%22http://www.mathworks.com/matlabcentral/answers/?term=tag%3A%22figure%22http://www.mathworks.com/matlabcentral/answers/?term=tag%3A%22data%22http://www.mathworks.com/matlabcentral/answers/?term=tag%3A%22extract%22http://www.mathworks.com/matlabcentral/profile/authors/4622813-mathworks-support-teamhttp://www.mathworks.com/matlabcentral/profile/authors/6041271-parrish-chhttp://www.mathworks.com/matlabcentral/answers/100687#answer_186949http://www.mathworks.com/matlabcentral/profile/authors/4622813-mathworks-support-teamhttp://www.mathworks.com/matlabcentral/answers/flagged/new?flaggable_id=100687&flaggable_type=Questionhttp://www.mathworks.com/matlabcentral/profile/authors/4622813-mathworks-support-team
  • 7/26/2019 How Do I Extract Data From MATLAB Figures_ - MATLAB Answers - MATLAB Central

    2/7

    4/12/2016 How do I extract data from MATLAB figures? - MATLAB Answers - MATLAB Central

    http://w ww.m athw or ks.com /m atl abcentr al /answ er s/100687- how -do- i- extr act- data- fr om -m atl ab- fi gur es

    3 Answers

    Vote

    6

    Flag Link

    Answer by MathWorks Support Team on 8 May 2015

    Accepted answer

    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.f ig'.

    1. Open the figure file:

    open('example.fig');

    %or

    figure;

    plot(1:10)

    2. Get a handle to the current figure:

    h = gcf; %current figure handle

    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'); %axes handlesdataObjs = get(axesObjs, 'Children'); %handles to lowlevel graphics objects in

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

    objTypes = get(dataObjs, 'Type'); %type of lowlevel graphics object

    NOTE : Different objects like 'Line' and 'Surface' will store data differently. Based on the 'Type', you

    http://www.mathworks.com/matlabcentral/profile/authors/4622813-mathworks-support-teamhttp://www.mathworks.com/matlabcentral/answers/flagged/new?flaggable_id=110036&flaggable_type=Answerhttp://www.mathworks.com/matlabcentral/profile/authors/4622813-mathworks-support-team
  • 7/26/2019 How Do I Extract Data From MATLAB Figures_ - MATLAB Answers - MATLAB Central

    3/7

    4/12/2016 How do I extract data from MATLAB figures? - MATLAB Answers - MATLAB Central

    http://w ww.m athw or ks.com /m atl abcentr al /answ er s/100687- how -do- i- extr act- data- fr om -m atl ab- fi gur es

    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:

    xdata = get(dataObjs, 'XData'); %data from lowlevel grahics objects

    ydata = get(dataObjs, 'YData');

    zdata = get(dataObjs, 'ZData');

    3 Comments

    I successfully used this code to extract data from a .fig file before.

    I attempted to extract data from another .fig file and I was unable to. I received this error

    message:

    Error using get Invalid property found. Object Name: text Property Name: 'XData'.

    Error in extract (line 23) xdata = get(dataObjs, 'XData') %data from low-level grahics objects

    I am new to MATLAB and I am unsure how to get this to work.

    Carlos on 8 May 2015

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

    xdata = get(lineObjs, 'XData');

    Walter Roberson on 8 May 2015

    Neat trick!

    dhhtr on 8 May 2015

    http://www.mathworks.com/matlabcentral/answers/flagged/new?flaggable_id=283784&flaggable_type=Commenthttp://www.mathworks.com/matlabcentral/profile/authors/5844942-dhhtrhttp://www.mathworks.com/matlabcentral/answers/flagged/new?flaggable_id=283780&flaggable_type=Commenthttp://www.mathworks.com/matlabcentral/profile/authors/434782-walter-robersonhttp://www.mathworks.com/matlabcentral/answers/flagged/new?flaggable_id=283776&flaggable_type=Commenthttp://www.mathworks.com/matlabcentral/profile/authors/5287446-carlos
  • 7/26/2019 How Do I Extract Data From MATLAB Figures_ - MATLAB Answers - MATLAB Central

    4/7

    4/12/2016 How do I extract data from MATLAB figures? - MATLAB Answers - MATLAB Central

    http://w ww.m athw or ks.com /m atl abcentr al /answ er s/100687- how -do- i- extr act- data- fr om -m atl ab- fi gur es

    Vote

    2

    Flag 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 findwhat you need.

    2 Comments

    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

    Cameron on 30 Jun 2014

    http://www.mathworks.com/matlabcentral/answers/flagged/new?flaggable_id=223278&flaggable_type=Commenthttp://www.mathworks.com/matlabcentral/profile/authors/5409444-cameronhttp://www.mathworks.com/matlabcentral/profile/authors/3419210-michaelhttp://www.mathworks.com/matlabcentral/profile/authors/3419210-michaelhttp://www.mathworks.com/matlabcentral/answers/flagged/new?flaggable_id=140762&flaggable_type=Answerhttp://www.mathworks.com/matlabcentral/profile/authors/3419210-michael
  • 7/26/2019 How Do I Extract Data From MATLAB Figures_ - MATLAB Answers - MATLAB Central

    5/7

    4/12/2016 How do I extract data from MATLAB figures? - MATLAB Answers - MATLAB Central

    http://w ww.m athw or ks.com /m atl abcentr al /answ er s/100687- how -do- i- extr act- data- fr om -m atl ab- fi gur es

    dataObjs = axesObjs.Children;

    seems to have done the trick. Hope this helps.

    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 Mdl on 26 Jun 2015

    Vote

    1

    Flag 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:

    http://www.mathworks.com/matlabcentral/profile/authors/6041271-parrish-chhttp://www.mathworks.com/matlabcentral/answers/flagged/new?flaggable_id=186949&flaggable_type=Answerhttp://www.mathworks.com/matlabcentral/profile/authors/6041271-parrish-chhttp://www.mathworks.com/matlabcentral/answers/flagged/new?flaggable_id=294691&flaggable_type=Commenthttp://www.mathworks.com/matlabcentral/profile/authors/6567933-julia-modl
  • 7/26/2019 How Do I Extract Data From MATLAB Figures_ - MATLAB Answers - MATLAB Central

    6/7

    4/12/2016 How do I extract data from MATLAB figures? - MATLAB Answers - MATLAB Central

    http://w ww.m athw or ks.com /m atl abcentr al /answ er s/100687- how -do- i- extr act- data- fr om -m atl ab- fi gur es

    Create a community profileto answer or comment on this question.

    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 useSurfaceData(3,1).XData to access the XData handle that is in the third index of the SurfaceData array.

    I hope this helps!

    0 Comments

    MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

    Learn more

    Discover what MATLAB can do for your career.

    Opportunities for recent engineering grads.

    http://makerzone.mathworks.com/?utm_source=Acad&utm_medium=spotlight&utm_campaign=MZhttps://www.mathworks.com/mwaccount/profiles/edit?uri=http%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fanswers%2F100687-how-do-i-extract-data-from-matlab-figures&t=community
  • 7/26/2019 How Do I Extract Data From MATLAB Figures_ - MATLAB Answers - MATLAB Central

    7/7

    4/12/2016 How do I extract data from MATLAB figures? - MATLAB Answers - MATLAB Central

    http://w ww.m athw or ks.com /m atl abcentr al /answ er s/100687- how -do- i- extr act- data- fr om -m atl ab- fi gur es

    Apply Today

    MATLAB AcademyNew to MATLAB?

    Learn MATLAB today!

    http://matlabacademy.mathworks.com/?s_tid=mlc_mlac_ans_slhttp://www.mathworks.com/company/jobs/opportunities/students/infographic.html?source=7677