uicontrol edit field .String doesn't visually update from KeyReleaseFcn unless a breakpoint is set

Hello!
I'm forcing uppercase text in a classic uicontrol edit field as the user types (need figure, not uifigure — too slow to create for my use case).
function minimal_example
fig = figure('MenuBar','none','NumberTitle','off');
edit_field = uicontrol('Parent', fig, 'Style', 'edit', ...
'Position', [50 50 150 30], ...
'KeyReleaseFcn', @forceUpper);
function forceUpper(hObject, ~)
upperStr = upper(hObject.String);
if ~strcmp(hObject.String, upperStr)
hObject.String = upperStr;
end
end
end
Expected: typing lowercase letters shows uppercase immediately.
Actual: Nothing visually updates — although edit_field.String is correctly 'ABC' internally when checked afterward. If I add any breakpoint inside forceUpper, it suddenly works correctly.
This looks like a repaint race condition: .String updates internally, but the widget doesn't redraw before control returns to the event queue.
Im using R2024
Do you have a fix for this? or is there another way to create the GUI faster than uifigure but in a way that it updates the string?

2 Comments

"not uifigure — too slow to create for my use case"
Rather than re-creating, perhaps just use UIFIGURE and set its visibility. That should be fast.
Thanks for your answer, but even setting visibility it is not fast enough for a recurring window on a productive environment

Sign in to comment.

 Accepted Answer

uicontrol for traditional figure has the property that the String property does not update visually until one of two circumstances:
  • the uicontrol loses focus; or
  • the uicontrol gets an Enter key sequence (possibly Carriage Return as well when Enter is distinct from Carriage Return)
Therefor the programmatic way to proceed is to deliberately set the focus elsewhere after processing each keystroke. However, setting the focus elsewhere will destroy the sense that the uicontrol has of where in the string it is processing -- so for example if the user had clicked into the text and was entering additional text in the middle, then after changing the focus the uicontrol's understanding of the current position would revert to the end of the string.
In your case, you should consider permitting the user to enter lowercase text, but then in the Callback change lowercase to uppercase all at once.

More Answers (0)

Categories

Products

Tags

Asked:

on 17 Aug 2026

Commented:

on 19 Aug 2026

Community Treasure Hunt

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

Start Hunting!