MATLAB: How to use the pop-up menu uicontrol object to change the background color of the pop-up menu immediately after an option is selected from the menu

backgroundcolorguihighlightMATLABpopupmenu

I am using a pop-up menu uicontrol object to change its background color based on the selection. I would like to have the background color change immediately after the option is chosen. This currently does not happen because, when the option is chosen, the default highlighting color is blue and once I click somewhere else in the figure window, the change is reflected. I essentially want the change be reflected as soon as I select an option in the popupmenu i.e. on mouse click, the background color should change.

Best Answer

The ability to use the pop-up menu uicontrol object to change its background color is not available in MATLAB. This is because of the focus being still in the pop-up menu after the selection and hence the changes are not reflected.
To work around this issue you can use one of the following three options:
1. If you have more than one uicontrol in the GUI, after changing the background color of the pop-up control, immediately call "uicontrol(handle)" for another uicontrol. This call takes the focus off the pop-up menu object and achieves the desired result.
2. If the GUI has only one object, create another dummy object of very small dimensions as follows and shift the focus to this object:
t=uicontrol('pos',[200 300 0.1 0.1]);
3. Alternatively, create a dummy object as above and make it invisible by setting the "Visible" property to "off" as mentioned below and :
t=uicontrol('pos',[200 300 100 100]);
set(t, 'Visible', 'off');