[MATLAB] Retrieve data from .fig files

In MATLAB, when you saved an image into a .fig file and then want to retrieve the data from it, you can do as follows:

First, get a handle to the input .fig image by either doing

open('display.fig');
h = gcf;  % handle to the current displayed image.

or do (relate .fig format to .mat format):

h = hgload('display.fig');   % also give you the handle to the figure file

Now h is the handle to your image file. When you close the window of the displayed image, h is deleted.

Next step is to go down the hierarchy from the figure handle –> axes object –> image object (contains the data you need)

axesObjs = get(h, 'Children');       % get the axes object from the handle
imageObj = get(axesobj, 'Children')  % get the image object from the axes object

Now that you have your image object, you are able to retrieve all source of information of your .fig image file. Within this ‘imageObj’ object, some major information:

XData: the x-dimension size of your image
YData: the y-dimension size of your image
CData: this is the matrix of your image pixel intensities

There are other properties stored in the image object such as transparency, etc. But most of the time we care about the size and content of our image file. Last step is to retrieve the info you need using the ‘get’ function:

image = get(imageObj, 'CData'); % give you the image data

The second argument could be any property contained in the image object. (e.g. XData, YData, CData, etc.)