How can I extract data from a .fig file in MATLAB?

后端 未结 4 2050
礼貌的吻别
礼貌的吻别 2021-01-01 11:53

I know this is really basic, but I am new to MATLAB. After opening a .fig file, how do you actually work with the plotted data in the command window? All I see is the plot.

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-01 12:33

    Try hgload and then poke around the graphics handle structure it returns. For example, if you plot and save the following:

    x=0:.01:10;
    y=sin(x);
    h=plot(x,y);
    saveas(h,'testfigure.fig');
    

    Clear your workspace, and open the saved figure using hgload:

    clear
    close all
    h=hgload('testfigure.fig');
    

    You can inspect the figure's handle h by calling

    get(h)
    

    Or delve further into the axes/titles/legends by calling

    ch=get(h,'Children');
    

    If you're using the code in my example, you should only have one child for the figure, which will be the axes. Call the children of the axes, and you should have one line.

    l=get(ch,'Children');
    

    Next, call the 'Xdata' and 'Ydata' fields of the line, and you have your original data.

    x=get(l,'Xdata');
    y=get(l,'Ydata');
    

    If you have a more complicated figure than just axes, it gets a little tougher. You'll need to explore each child to determine if it's the plot you wanted to extract data from.

提交回复
热议问题