Question

I'm writing a script to resize any figure given to me, and I'd like to get the rows and columns of a subplot. For instance, if someone gave me a figure created by:

hfig = figure;
haxes = subplot(3,4,1);
...

Is there a property in the figure or axes handles that tells me that the figure has 3 rows and 4 columns? I need to resize each axis, and knowing the rows and columns is important so I know how to resize them.

Was it helpful?

Solution

No, there is no such property. There is only 'Position' property for axes object. What subplot function does is calculate proper axes position.

You can find all the axes on a figure with ax = findobj(gcf,'type','axes');. Then get positions with get(ax,'position') and analyze them for number of rows and columns.

For example:

pos = cell2mat(get(ax,'position'));
nrows = numel(unique(pos(:,2))); % the same Y position means the same row
ncols = numel(unique(pos(:,1))); % the same X position means the same column

Notice however that it will analyze only existing axes. If only two axes were created with subplot(221) and subplot(222) you will get 1 row, not 2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top