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);

 Réponse acceptée

Teja Muppirala
Teja Muppirala le 21 Mai 2011
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 commentaires

Teja Muppirala
Teja Muppirala le 21 Mai 2011
This is ok too (if you are allowing multiple or empty selections)
set(PNUList,'String',PNUnames, 'Value', []);
roi
roi le 23 Mai 2011
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é le 17 Juin 2019
Modifié(e) : Thomas Côté le 17 Juin 2019
10000 years later, thank you.
It was helpful for my own code.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by