Retrieve data from .fig matlab figure file

1. If your fig file is opened, pull it to active current window.

To find what data you want, check properties: get(get(gca,’Children’)) .

Let’s say, you have a curve and want get data ‘XData’ and ‘YData”.

x = get(get(gca,’Children’),’XData’);

y = get(get(gca,’Children’),’YData’);

If you plot(x,y), you’ll get the same the same curve.

If your figure is not opened yet. You can open it and do 1.

We can also use gcf. gcf is the handle of the current figure; gca is the handle of the current axis.

“gca” is the same as “get(gcf,’Children’) “.

That means, we can also do this:

get(get(gcf,’Children’),’Children’),’XData’)

We can also break it down to steps with struct or objects using gca or gcf.

  • hAxes = get(gca);
  • hProperties = hAxes.Children;
  • x = get(hProperties, ‘XData’);

or

  • hFig  = get(gcf);     %save figure handle object to a struct
  • hAxes = hFig.Children;
  • hProperties = get(hAxes);
  • hLine = hProperties.Children;
  • x = get(hLine, ‘XData’);

2. We can open the figure and store the handle to a variable:

fighandle = openfig(‘myfigure.fig’), which is basically the same as “gcf” if we open the figure first. Then we can do the same thing as in 1.

get(get(fidhandle,’Children’),’Children’),’XData’)

3. We don’t open the figure, but load the figure file into a struct variable.

myFigStruct = load(myfigure.fig’,’-MAT’) ;

or

myFigStruct = load(‘-MAT’,myfigure.fig’);

Note: the root element of the figure is called ‘hgS_070000′ for
Matlab7+ figures – it’s called something else for figures saved in
Matlab6- (probably ‘hgS_06…’ or something).

Then find the data element in the hierarchy, like ‘XData’:

myFigStruct.hgS_070000.children.children.properties.XData

4. Save the data of the figure file to a .mat file.

  • open .fig file.
  • saveas(gcf,’mydata’,’mmat’)   %save data of the .fig file to .mat file called mydata.mat
  • load ‘mydata’
  • The object variable  ‘mat’ now contains all your data. To extract it from it, do: x1=mat{1}, x2=mat{2} etc.

5.  If the figure is a line, we can also use findobj or findall from the figure handle.

fighandle=openfig(‘myfigure.fig’);
ax=findall(fighandle,’Type’,’line’);
x=get(ax,’XData’);
y=get(ax,’YData’);

or

s=hgload(‘myfigure.fig’);
h = findobj(s,’Type’,’line’);
x=get(h,’xdata’);
y=get(h,’ydata’);

About these ads

14 Responses

  1. This is very useful – thank you.

  2. hi,I am work by ice and guide function in matlab.but I cann’t work them and I don’t know ice.fig file please help me tanks alot

    • Could you please describe your problem more detailedly?

      • I cann’t work by ice function. what ‘s method work of this function ?tanks alot

      • I don’t find any ice function in matlab. Is it because I’m using different matlab version? And, could you let me know what you want to do with ice functions? You mentioned guide before. You have problems using guide also? Is it the same one problem with ice?

  3. hi,I am work by ice and guide function in matlab.but I ate my trouble. please help me.tanks alot.

  4. Thank you, your article was very helpful for me

  5. Thanks a lot!! I had the figure but I lost the data, so your article was very helpful.

  6. [...] Another alternative was to use each figure handle and access the underlying data through their XData and YData fields. But this was not the most elegant solution. Then we remembered that figure handles are [...]

  7. Nice contribution.

  8. Gratters your tutorial is very usefull. thanks!

  9. Hi, can i get almost plot points? in my case i get a “stairs plot”, so i have points that i dont really have on my plotted vector. Thanks.

    • Stairs points? That’s the original data points? You want smooth curve, right? That’s interpolated. If you plot with excel, you can also see different types of scatter plot.

  10. Another thanks, this was very helpful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 65 other followers

%d bloggers like this: