Sack11
Sack11

Reputation: 173

How to extract a matrix from a .fig in Matlab?

I have a Matlab .fig file. (Basically a pcolor plot). I want to extract the matrix (like rows and columns into an array variable) from this image. How do I do this? Thanks for any inputs or pointers.

Upvotes: 1

Views: 1063

Answers (2)

In addition to Luis Mendo's answer, I want to point out that MATLAB supports dot-notation and if performance is any concern, dot-notation should always be preferred over the set()/get() methods.

Using the handle() function wrapper along with dot-notation is fastest for both setting and getting of handle class properties.

>> x=magic(3)

x =

     8     1     6
     3     5     7
     4     9     2

>> pcolor(x)
>> ax = handle(gca);
>> ax.Children.CData

ans =

     8     1     6
     3     5     7
     4     9     2

For timing experiments and details see: Undocumented MATLAB

Upvotes: 0

Luis Mendo
Luis Mendo

Reputation: 112659

The axes have a child, which is an object of type surface if you used the pcolor function, or of type image if you used the image function. The matrix is in the CData property of this object:

>> x = magic(3) % example data
x =
     8     1     6
     3     5     7
     4     9     2
>> pcolor(x) % generate image
>> get(get(gca,'Children'),'CData') % retrieve the data
ans =
     8     1     6
     3     5     7
     4     9     2

Upvotes: 2

Related Questions