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. I'm not sure how to actually get the data.
4 Answers
Actually, you don't even have to display the figure in order to get the data. FIG files are stored in the standard Matlab MAT format, that you can read using the built-in load() function. The figure handles and data are stored in a structure that you can easily understand and process.

- 5,704
- 31
- 34
-
4I posted a detailed article about this: http://undocumentedmatlab.com/blog/fig-files-format/ – Yair Altman Dec 22 '10 at 19:44
-
I get the following error: Unknown text on line number 1 of ASCII file DischargeFigure.fig "MATLAB". Any clue? – Millemila Aug 12 '20 at 02:47
Here's a really simple way:
Click on the object that you want to get the data from. There will be no indication that you have clicked on it.
>> xd = get(gco,'XData');
>> yd = get(gco,'YData');
Sometimes it can be hard to click on the line, or other object, itself. If you have this problem, click on the axes that contains the child(ren) you are interested in, then:
>> kids = get(gca,'Children');
This will give you an array of handles to the various children. You can try getting them one at a time by indexing into kids, or use the following to get all data at once. This will return the results as a cell array, which can be a little tricky if you haven't used them before:
>> xd = get(kids,'XData');
>> yd = get(kids,'YData');
>> xd1 = xd{1}; %# X Data from first line

- 125,304
- 15
- 256
- 359

- 131
- 2
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.

- 7,398
- 14
- 47
- 61
-
1You can also use the [FINDOBJ](http://www.mathworks.com/access/helpdesk/help/techdoc/ref/findobj.html) function to search for a graphics object with a given set of parameter-value pairs instead of having to step your way through the parent-child hierarchy. This would be most effective if the `'Tag'` properties of the objects were set to unique identifiers *before* being saved as a .fig file. – gnovice Jun 04 '10 at 18:56
-
4You can just search for line-handles directly (though you should close the legend first). For example: `lineH = findobj(h,'type','line')`. If you have multiple lines, and you only want the red one, you can use `findobj` with multiple search criteria, such as `redLineH = findobj(h,'type','line','color','r');` – Jonas Jun 04 '10 at 19:59
-
@gnovice and @Jonas - I think I had run across findobj before, but I didn't even think to apply it to this situation. Great suggestions! – Doresoom Jun 04 '10 at 20:06
-
how do I include several plots in one figure in that above example? – Asking Questions Oct 20 '16 at 11:32
-
@AskingQuestions It depends if you want different line plots on one graph, or if you want separate axes in the same figure. For the former, use hold all, for the latter, use subplot. MathWorks has great documentation on both. – Doresoom Oct 21 '16 at 21:42
Use the HGLOAD command. Reference available here.

- 125,304
- 15
- 256
- 359

- 16,900
- 4
- 29
- 46