How to extract data from figure in matlab?

2020-07-16 03:08发布

问题:

I have saved different Matlab plots in an unique .fig. The figure is like this: Now, I would like to introduce a filter in these plots to reduce the noises, but unfortunately I have lost the code that generates these signals.
Is there a way to extract data of each signal in this figure? I tried this:

open('ttc_delay1000.fig'); 
h = gcf; %current figure handle
axesObjs = get(h, 'Children');  %axes handles
dataObjs = get(axesObjs, 'Children'); %handles to low-level graphics objects in axes

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

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

But I am confused and I don't know if it's the right way to act. Thanks!

回答1:

A one-liner for your problem:

data = get(findobj(open('ttc_delay1000.fig'), 'Type','line'), {'XData','YData'});

The steps are there (from the inner calls to the outer calls):

  • open the file;
  • look into it for the line series;
  • return the data.

data{n,1} will contain the XData of the LineSeries number n, wile the data{n,2} will contain the YData of the said LineSeries.

If you want to smooth the lines directly in the figure, the idea is the same:

    %//Prepare moving average filter of size N
    N = 5;
    f = @(x) filter(ones(1,N)/N, 1, x);

    %//Smooth out the Y data of the LineSeries
    hf = open('ttc_delay1000.fig');
    for hl = transpose(findobj(hf,'Type','line'))
            set(hl, 'YData', f(get(hl,'YData')));
    end;
    saveas(hf, 'ttc_delay1000_smooth.fig');