Why does my popupmenu disappear when I SET the string property?
조회 수: 3 (최근 30일)
이전 댓글 표시
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.
채택된 답변
MathWorks Support Team
2009년 6월 27일
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);
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!