Saturday, September 1, 2012

Converting Structures to Matrices in Matlab

I use imfeature a lot (now regionprops). Since the output of imfeature(regionprops) is a structure I sometimes need to access the values of a feature for all blobs preferably in matrix form. I am not so sure if it can be done in one line but what I've discovered is a two step process.

Suppose a call to regionprops is made as follows:

stats = regionprops(L,'Area','MajorAxisLength','MinorAxisLength');

The first step is to convert the structure into a cell:
c = struct2cell(stats);

Then the cell is converted into a matrix:
m = cell2mat(c);

m now would have the same number of rows as features and columns would be as many as the number of blobs processed.