correctly erasing items from a listbox

I am trying to erase some listings from a listBox. however, when I push the appropriate button I receive the following warning:
Warning: multi-selection listbox control requires that Value be an integer within String range
and the listBox control doesn't get rendered. Any ideas for a fix? here is the code of the button's callback :
function btnRemovePNU_FromUse_Callback(hObject, eventdata, handles)
% hObject handle to btnRemovePNU_FromUse (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
PNUList = handles.liPNU_toUse;
indexedPNU = get(PNUList,'value');
PNUnames = get(PNUList,'String');
PNUnames(indexedPNU) = [];
set(PNUList,'String',PNUnames);

 채택된 답변

Teja Muppirala
Teja Muppirala 2011년 5월 21일

1 개 추천

After you delete the items from the 'String' property, you need to update the 'Value' property as well.
For example, if you initially had 3 items, and you selected the 3rd one, then 'value' is 3. Then you delete it, and now you have two items left, but the value is still = 3. This is the problem.
Update the 'value' and you'll be fine:
set(PNUList,'String',PNUnames, 'Value', 1);

댓글 수: 3

Teja Muppirala
Teja Muppirala 2011년 5월 21일
This is ok too (if you are allowing multiple or empty selections)
set(PNUList,'String',PNUnames, 'Value', []);
roi
roi 2011년 5월 23일
Thanks Teja!
I took your answer and updated the value to be at one place before the erased text:
function btnRemovePNU_FromUse_Callback(hObject, eventdata, handles)
% hObject handle to btnRemovePNU_FromUse (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
PNUList = handles.liPNU_toUse;
indexedPNU = get(PNUList,'value');
newPlace = indexedPNU(1)-1;
if (newPlace <=0) newPlace = 1; end
PNUnames = get(PNUList,'String');
if ~isempty(PNUnames)
PNUnames(indexedPNU) = [];
set(PNUList,'String',PNUnames,'value', newPlace);
end
Thomas Côté
Thomas Côté 2019년 6월 17일
편집: Thomas Côté 2019년 6월 17일
10000 years later, thank you.
It was helpful for my own code.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

질문:

roi
2011년 5월 21일

편집:

2019년 6월 17일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by