MATLAB: Clear Strings in a PopUpMenu

clearguipopupmenustrings

So I have built a GUI with a popupmenu. Say that the strings in the popupmenu are {'1','2','3'}. In this same GUI, I have a button that is supposed to repopulate the strings in the popupmenu. Say, that I push the button and instead of populating the popupmenu with {'1','2','3'}, I push the button and I want to populate it with {'5','4'}. The result is now a popupmenu with strings {'5','4','3'}. What I want to know is either 1. How do I clear the strings in the popupmenu before populating it, or 2. How do I get rid of the entry (the last 3 in this case) that is unwanted. Thanks in advance for your help.

Best Answer

You can control what appears in the popup menu by using a cell array containing the strings you want to display.
For instance you don't have to clear the existing popup menu's content but rather replace it with what you want:
MyStrings = {'5' '4'};
set(handlesToPopupMenu,'String',MyStrings);
and it should update correctly. If on the other hand you want to retrieve the content of the popup menu as a cell array, use this:
MyStrings = cellstr(get(handlesToPopupMenu,'String'));