How to get absolute aspect ratio of a figure?

How to get absolute (in screen pixel units) aspect ratio of an existing figure window? or the plotting area?

Answers (1)

I am not sure what an absolute aspect ratio is, but in any case:
fig = handle to graphics container object of interest such as figure
oldunits = get(fig, 'Units');
set(fig, 'Units', 'pixels');
figpos = get(fig, 'Position');
set(fig, 'Units', oldunits);
aspect_ratio = figpos(3)/figpos(4);

2 Comments

What is absolute aspect ratio?
Example:
Making full screen:
figure('units','normalized','outerposition',[0 0 1 1]);
Here 1:1 is not so informative, so I want the absolute aspect ratio in pixels = aspect ratio of the full screen.
Setting a figure to 'normalized' [0 0 1 1] does not cover the full screen: full screen hides window decorations and some operating-system imposed margin. If you want full screen then that is a different question than what you get from normalized 0 0 1 1.
The code I provided above finds the window size in pixels and calculates the aspect ratio.
If you want to know the aspect ratio of the screen then,
figpos = get(0,'ScreenSize');
aspect_ratio = figpos(3)/figpos(4);
but only for R2014a and before. For later versions,
figpos = get(groot,'ScreenSize');
aspect_ratio = figpos(3)/figpos(4);
Note that this code does not handle the case of multiple displays. For those I think the code would be
aspect_ratio = figpos(:,3)./figpos(:,4);

Sign in to comment.

Asked:

on 5 Oct 2015

Commented:

on 6 Oct 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!