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’);

Advertisements