MATLAB: Does the popupmenu disappear when I SET the string property

guideMATLABpopupmenu

I have two popupmenus in my GUI. The strings in the second popupmenu are dependent upon the selection made in the first popup menu.
In the following example, the first string property of the first popupmenu is set to [{'trial1'}, {'trial2'}, {'trial3'}].
function popupmenu1_callback(h, eventdata, handles, )
val = get(handles.popupmenu1, 'value');
switch val
case 1
str = [{'case1'}];
case 2
str = [{'case1'}, {'case2'}];
case 3
str = [{'case1'}, {'case2'}, {'case3'}];
end
set(handles.popupmenu2, 'string', str);
When I select 'trial2' in popupmenu1, I am allowed to select 'case1' and 'case2' from popupmenu2. I then select 'case2' from popupmenu2. When I then select 'trial1', popupmenu2 disappears.
I also receive the following warning:
Warning: popupmenu control requires that Value be an integer within String range
Control will not be rendered until all of its parameter values are valid.
Warning: popupmenu control requires that Value be an integer within String range
Control will not be rendered until all of its parameter values are valid.

Best Answer

This error occurs because the selected 'case' does not exist in the array defined for the selected 'trial'. When you select 'trial2' and 'case2', the 'case' corresponds to a value of 2 for popupmenu2. When you then select 'trial1', the value for the 'case' is still set to 2 even though the string array associated with 'trial1' contains one element. MATLAB attempts to index into this array with the value 2 and fails.
The value for popupmenu2 must be set to a "common" index before setting the string property. In this case, the common index is 1. To set the value, refer to the following example:
set(handles.popupmenu2, 'value', 1);
set(handles.popupmenu2, 'string', str);