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

21 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.

  11. Hi,
    I am getting error while doing plot on the extracted variables.
    My code:

    “f2 = figure (‘Name’, ‘Temporary Fig’ );
    copyobj ( handles.axes2, f2 );
    x = get(get(get(gcf,’Children’),’Children’),’XData’);
    y = get(get(get(gcf,’Children’),’Children’),’YData’);
    plot(handles.axes3,x,y);”

    can you please help on this?

    Error:
    Error using plot
    Not enough input arguments.
    Error in sample_gui>pushbutton6_Callback (line 212)
    plot(handles.axes3,x,y);

    • So you want to plot the data in another figure, e.g. f3? Did you get the handle to the axes of f3 for handles.axes3?
      I tested with some arbitrary data, and didn’t get any problem. The code I tried is the following:

      load mri
      imagesc(D(:,:,1,1))
      handles.axes2 = gca;
      f2 = figure('Name','Temporary Fig');
      copyobj(handles.axes2, f2);
      x = get(get(get(gcf,'Children'),'Children'),'XData');
      y = get(get(get(gcf,'Children'),'Children'),'YData');
      figure(3)
      handles.axes3 = gca;
      plot(handles.axes3,x,y)

      If this is different than what you do, please let me know what you do, I’ll test again.

      • Hi Sir,

        Thanks for your quick reply.
        My problem:
        I have created 3 axes in a gui.
        I made plot into first axes using some data i have.
        I did FFT on the data I have and plot into second axes.

        I need to round some places in the second axes and need to perform IFFT on it and need to display in 3rd axes.

        I tried in the above way by getting data into x and y variables from second axes, so i can perform IFFT on new data i will have, but unable to do.

        Can you please help me on this?
        when i used the below code, cells are getting created. I hope i am getting the plot error because of that.
        x = get(get(get(gcf,’Children’),’Children’),’XData’);
        y = get(get(get(gcf,’Children’),’Children’),’YData’);

        I tried the code you sent above. I didn’t get any error, but the figures are different. I got one big line in the last figure;

  12. The figures are different because the original figure is an image, which has X and Y data, and color map. I only obtained X,Y data and plotted in Fig. 3. That’s why it’s a line. Do you have a line or an image?

    If you do fft and plot it in 2nd figure. How did you plot it? Raw fft data is complex. Be careful with that.

    If you still have the problem, can you send me the exact data you want to plot in figure 1, 2, and 3? I’ll test it for you.

    • Hi Sir,
      this is the code which i used after creating gui.
      I dont know how to send the data i used.

      function varargout = sample_gui(varargin)
      % SAMPLE_GUI MATLAB code for sample_gui.fig
      % SAMPLE_GUI, by itself, creates a new SAMPLE_GUI or raises the existing
      % singleton*.
      %
      % H = SAMPLE_GUI returns the handle to a new SAMPLE_GUI or the handle to
      % the existing singleton*.
      %
      % SAMPLE_GUI(‘CALLBACK’,hObject,eventData,handles,…) calls the local
      % function named CALLBACK in SAMPLE_GUI.M with the given input arguments.
      %
      % SAMPLE_GUI(‘Property’,’Value’,…) creates a new SAMPLE_GUI or raises the
      % existing singleton*. Starting from the left, property value pairs are
      % applied to the GUI before sample_gui_OpeningFcn gets called. An
      % unrecognized property name or invalid value makes property application
      % stop. All inputs are passed to sample_gui_OpeningFcn via varargin.
      %
      % *See GUI Options on GUIDE’s Tools menu. Choose “GUI allows only one
      % instance to run (singleton)”.
      %
      % See also: GUIDE, GUIDATA, GUIHANDLES

      % Edit the above text to modify the response to help sample_gui

      % Last Modified by GUIDE v2.5 25-Mar-2015 12:15:52

      % Begin initialization code – DO NOT EDIT
      gui_Singleton = 1;
      gui_State = struct(‘gui_Name’, mfilename, …
      ‘gui_Singleton’, gui_Singleton, …
      ‘gui_OpeningFcn’, @sample_gui_OpeningFcn, …
      ‘gui_OutputFcn’, @sample_gui_OutputFcn, …
      ‘gui_LayoutFcn’, [] , …
      ‘gui_Callback’, []);
      if nargin && ischar(varargin{1})
      gui_State.gui_Callback = str2func(varargin{1});
      end

      if nargout
      [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
      else
      gui_mainfcn(gui_State, varargin{:});
      end
      % End initialization code – DO NOT EDIT

      % — Executes just before sample_gui is made visible.
      function sample_gui_OpeningFcn(hObject, eventdata, handles, varargin)
      % This function has no output args, see OutputFcn.
      % hObject handle to figure
      % eventdata reserved – to be defined in a future version of MATLAB
      % handles structure with handles and user data (see GUIDATA)
      % varargin command line arguments to sample_gui (see VARARGIN)

      % Choose default command line output for sample_gui
      handles.output = hObject;

      % Update handles structure
      guidata(hObject, handles);

      % UIWAIT makes sample_gui wait for user response (see UIRESUME)
      % uiwait(handles.figure1);

      % — Outputs from this function are returned to the command line.
      function varargout = sample_gui_OutputFcn(hObject, eventdata, handles)
      % varargout cell array for returning output args (see VARARGOUT);
      % hObject handle to figure
      % eventdata reserved – to be defined in a future version of MATLAB
      % handles structure with handles and user data (see GUIDATA)

      % Get default command line output from handles structure
      varargout{1} = handles.output;

      % — Executes on button press in pushbutton4.
      function pushbutton4_Callback(hObject, eventdata, handles)
      % hObject handle to pushbutton4 (see GCBO)
      % eventdata reserved – to be defined in a future version of MATLAB
      % handles structure with handles and user data (see GUIDATA)
      Data = load(‘Data.mat’);
      plot(handles.axes1,Data.Longitude,Data.Data)
      set(handles.axes1,’XMinorTick’,’on’)
      grid on
      set(handles.pushbutton5,’enable’,’on’);
      setappdata(hObject.Parent,’dataForFft’,Data.Data);
      setappdata(hObject.Parent,’longitudeForFft’,Data.Longitude);

      % — Executes on button press in pushbutton5.
      function pushbutton5_Callback(hObject, eventdata, handles)
      % hObject handle to pushbutton5 (see GCBO)
      % eventdata reserved – to be defined in a future version of MATLAB
      % handles structure with handles and user data (see GUIDATA)
      x = getappdata(hObject.Parent,’dataForFft’);
      y = getappdata(hObject.Parent,’longitudeForFft’);
      p= fft(x);
      plot(handles.axes2,y,p);
      set(handles.pushbutton6,’enable’,’on’);

      % — Executes on button press in pushbutton6.
      function pushbutton6_Callback(hObject, eventdata, handles)
      % hObject handle to pushbutton6 (see GCBO)
      % eventdata reserved – to be defined in a future version of MATLAB
      % handles structure with handles and user data (see GUIDATA)
      f2 = figure (‘Name’, ‘Temporary Fig’ );
      copyobj ( handles.axes2, f2 );
      x = get(get(get(gcf,’Children’),’Children’),’XData’);
      y = get(get(get(gcf,’Children’),’Children’),’YData’);
      handles.axes3 = gca;
      plot(handles.axes3,x,y);

      • if you send mail on pavan.moturu@gmail.com, I will share the data sir.

        thank you

      • Since some code was missing, I tested the code below:

        figure(1)
        handles.axes1 = gca;
        figure(2)
        handles.axes2 = gca;
        plot(handles.axes1,Longitude,Data)
        p = fft(Data);
        plot(handles.axes2,Longitude,p);

        f2 = figure('Name','Temporary Fig');
        copyobj(handles.axes2, f2);
        x = get(get(get(gcf,'Children'),'Children'),'XData');
        y = get(get(get(gcf,'Children'),'Children'),'YData');
        x = cell2mat(x);
        % x = x(1,:);
        y = cell2mat(y);
        figure(4)
        handles.axes3 = gca;
        plot(handles.axes3,x,y);

        I reproduced your error message “Error using plot: Not enough input arguments.” If you check your x and y, you’ll see they are arrays of values, but cells. As I mentioned earlier, p = fft(Data) is a complex. You need be careful with that. When you plot(Longitude,p), it shows “Warning: Imaginary parts of complex X and/or Y arguments ignored”. And Data is 2050×197, p is 2050×197. So plot(Longitude,p) plots 2050 lines which only include real part of p. Then when you retrieve x and y, you retrieve 2050 cells.

        If you do

        f2 = figure('Name','Temporary Fig');
        copyobj(handles.axes2, f2);
        x = get(get(get(gcf,'Children'),'Children'),'XData');
        y = get(get(get(gcf,'Children'),'Children'),'YData');
        x = cell2mat(x);
        % x = x(1,:);
        y = cell2mat(y);
        figure(4)
        handles.axes3 = gca;
        plot(handles.axes3,x,y);

        It will plot.
        Keep in mind, this way, you’ll only recover the real part of p, but the original p, because you lost the imaginary part.

Leave a reply to Fernando Cancel reply