MATLAB: Does the callback for the edit box in the GUI execute twice in MATLAB 7.0.1 (R14SP1)

boxeditguiMATLABstring

When I change the value of the 'String' property in the edit box callback function, the callback is executed a second time when the edit box loses focus.

Best Answer

This is expected behavior. The callback event for an edit box is triggered by changing the String property and then changing the object focus.
To work around this issue, set a flag in the callback procedure that prevents the entire callback from being executed once the edit box loses focus.
For example:
function myedit_callback(hObject, eventdata, handles)
persistent myEditboxFlag
if isempty(myEdtiboxFlag)
myEditboxFlag = true;
end
if myEditboxFlag
%perform callback procedure
set(hObject, 'String', 'A new string')
myEditboxFlag = false;
else
myEditboxFlag = false;
end