MATLAB: How to resize a uicontrol dynamically, depending on the length of the displayed string, or once at the initialization depending on the longest string in the ‘String’ property

arraycelldynamicguilengthMATLABpop-uppopupmenusize;stringuicontrol

I am using a uicontrol pop-up menu and want to set the size of the uicontrol large enough to hold all possible options. The right size should be as small as possible to neatly display all the contents. I use the 'Units' property 'characters'.

Best Answer

The ability to change the size of uicontrol objects dynamically is not available in MATLAB.
To work around this issue, you can use the attached file "simple_extent_dynamic.m". This function will create a uicontrol object in the manner described in the summary and dynamically adjust the size of your pop-up menu. The size can be restricted with upper and/or lower bounds (commented), because of appearance, but it is possible to run the function without bounds too.
The attached file "simple_extent.m" finds the longest string of the cell array which should be displayed in the pop-up menu. Then it resizes the uicontrol with a certain calculation. You can also use bounds such as those shown in the function "simple_extent_dynamic.m".
Note: Both functions would need to be edited for a FontSize other than the default one and for configurations other than Windows XP 96 DPI.