Syntax
textadjust2gca(hh)
Input Parameter
Examples
SGfigure; axis off; h=textP([-.2 0],'The quick brown fox jums over the lazy dog','','',32);
textadjust2gca(h)
Copyright 2021-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 algorithm is designed to adjust the width of a text object to fit within the current axes (gca) in a MATLAB figure. It is particularly useful when the text does not fit on the screen width, such as in video titles.
Input Parameters
- hh: Handle to the text object that needs adjustment.
Algorithm Steps
- Set the size variable to 0.95, indicating that the text should occupy a maximum of 95% of the axes width.
- Change the units of the text object
hh
to 'pixels' to facilitate size calculations.
- Retrieve the current extent of the text object using
get(hh, 'Extent')
, which provides the size and position of the text.
- Store the current units of the axes using
get(gca, 'Units')
to restore them later.
- Set the units of the axes to 'pixels' to ensure consistent measurement units.
- Retrieve the position of the axes using
get(gca, 'Position')
.
- Enter a while loop that continues adjusting the text size until it fits within the specified size of the axes:
- Check if the width of the text (ts(3)) plus its horizontal position (ts(1)) exceeds 95% of the axes width (as(3) * size).
- If the text position is negative, adjust it to start from zero on the x-axis.
- Decrease the font size of the text by 1 unit using
set(hh, 'FontSize', dd-1)
.
- Update the position and extent of the text after changing the font size.
- Once the text fits, restore the original units of the axes using
set(gca, 'Units', uuu)
.
Algorithm explaination created using ChatGPT on 2025-08-19 00:42. (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