getgcapixelsize

by Tim C. Lueth, SG-Lib Toolbox: SolidGeometry 5.6 - User interface
Introduced first in SolidGeometry 3.8, Creation date: 2017-04-02, Last change: 2025-08-19

returns the position of the gca in the current figure

Description

This function is required if getframe(gca) does not work anymore.
In this case try getframe(gcf,getgcapixelsize)

See Also: setgcapixelsize

Example Illustration

 missing image of getgcapixelsize

Syntax

rect=getgcapixelsize

Output Parameter

rect: [left bottom width height] in pixels

Examples


subplot(2,2,2)
rect=getgcapixelsize
I=getframe(gcf,rect); figure(3333); imshow(I.cdata);




Copyright 2017-2025 Tim C. Lueth. All rights reserved. The code is the property of Tim C. Lueth and may not be redistributed or modified without explicit written permission. This software may be used free of charge for academic research and teaching purposes only. Commercial use, redistribution, modification, or reverse engineering is strictly prohibited. Access to source code is restricted and granted only under specific agreements. For licensing inquiries or commercial use, please contact: Tim C. Lueth

Algorithm (Workflow)

This function, getgcapixelsize, calculates the pixel size and position of the current axes (gca) within a figure in MATLAB. It is useful when the getframe(gca) function does not work as expected.

Input Parameters

The function does not take any input parameters.

Output

The function returns a vector rect which contains four elements: [left, bottom, width, height] in pixels.

Algorithm Steps

  1. Retrieve the position of the current axes (gca) in terms of percentage of the figure using get(gca, 'Position'). This returns a vector app with four elements: [left, bottom, width, height] in percentage.
  2. Get the inner position of the parent of the current axes, which is the figure, in pixels using get(get(gca, 'Parent'), 'InnerPosition'). This returns a vector fp with four elements: [left, bottom, width, height] in pixels.
  3. Calculate the position and size of the axes in pixels by multiplying the percentage values by the corresponding figure dimensions in pixels. This is done using the formula: ap = [app(1)*fp(3), app(2)*fp(4), app(3)*fp(3), app(4)*fp(4)].
  4. Round the calculated pixel values to the nearest integer using round(ap) to get the final pixel position and size of the axes.
  5. Return the rounded pixel values as the output rect.

Example Usage

The function can be used in a MATLAB script as follows:


subplot(2,2,2)
rect = getgcapixelsize;
I = getframe(gcf, rect);
figure(3333);
imshow(I.cdata);

In this example, a subplot is created, and the pixel size of the axes is obtained using getgcapixelsize. The getframe function is then used to capture the content of the axes, and the captured image is displayed in a new figure.

Algorithm explaination created using ChatGPT on 2025-08-18 23:38. (Please note: No guarantee for the correctness of this explanation)

Last html export of this page out of FM database by TL: 2025-09-21