Respuesta aceptada

Image Analyst
Image Analyst el 14 de Dic. de 2011

4 votos

You have to get the 'String' value of the listbox, which will be a cell array.
currentItems = get(hListbox, 'String');
Set the row for the item you want to remove equal to[].
newItems = currentItems;
newItems(rowToDelete) = []; % Or something like that - maybe it's {}
Then send the cell array back to the listbox with the
set(hListbox, 'String', newItems)
function.

1 comentario

Walter Roberson
Walter Roberson el 14 de Dic. de 2011
Note:
if get(hListbox,'ListboxTop') >= min(rowToDelete)
then you will want to set(hListbox, 'ListboxTop') to a different value as otherwise it will point to the wrong place or point past the end of the new list.

Iniciar sesión para comentar.

Más respuestas (1)

Maryam Emad
Maryam Emad el 15 de Dic. de 2011

0 votos

Thanks a lot "Image Analyst" ..
I try to use your code , it is work good and remove specific items.
But, when I want to delete the last item , listbox was deleted as a whole!!!
Pleas Help me :(

8 comentarios

Image Analyst
Image Analyst el 16 de Dic. de 2011
You will need to set the value of the listbox to something less than "end". For example, if the listbox "string" property had 10 cells (items) in it, and you clicked on the last one, so now the "value" property has a value of 10, then if you delete the 10th item and try to stick it back it, the string property will have only 9 cells, but your value property still has a value of 10, which is beyond the end of the list which now has only 9 items in it. So you'd need to set the value property to some value between 1 and 9 (i.e. the old "end" minus 1) so that the selected item is a valid item number. The value property doesn't automatically get updated for the new list. If you don't, your listbox won't appear. Does that explain it? It's like what Walter said but I'd set the value property instead of the ListboxTop property.
Amani
Amani el 19 de Dic. de 2011
Thanx aloot ,,
but I'm not understand ><
maybe you mean that ?
currentItems = get(handles.listbox1, 'String');
rowToDelete = get(handles.listbox1, 'value');
newItems = currentItems;
newItems(rowToDelete-1) = [];
set(handles.listbox1, 'String', newItems)
right :\ ?
Walter Roberson
Walter Roberson el 19 de Dic. de 2011
I would not think so, no.
currentItems = get(handles.listbox1,'String');
rowtoDelete = get(handles.listbox1, 'Value');
newItems = currentItems;
newItems(rowToDelete) = [];
set(handles.listbox1, 'String', newItems, 'Value', 1);
Image Analyst
Image Analyst el 19 de Dic. de 2011
No. Try this (untested):
currentItems = get(handles.listbox1, 'String');
% Store which items they highlighted.
rowToDelete = get(handles.listbox1, 'value');
% Deselect any items to prevent problems after deletion.
set(handles.listbox1, 'Value', []);
% Initialize new list to be the same as the old list.
newItems = currentItems;
% Remove highlighted items from the list
newItems(rowToDelete) = [];
% Send shortened list back to the listbox control.
set(handles.listbox1, 'String', newItems);
% Select the first item on the list.
if ~isempty(newItems)
if length(newItems) >= 1
set(handles.listbox1, 'Value', 1);
end
end
Walter Roberson
Walter Roberson el 19 de Dic. de 2011
Value of 1 is legal when the String is completely empty, so there is no need to do the checking shown above: just setting to 1 like I showed will work.
Amani
Amani el 19 de Dic. de 2011
Thaaaaanx alot Walter .. it is okaaay now ^_^
thanx thanx Image Analyst : )
Jihad Chamseddine
Jihad Chamseddine el 14 de Jul. de 2014
I don't know if you guys are still in this page, but I want to ask you if I want when I remove an item from the listbox so they will be renumbered automatically, can that be done? for example I have items numbered from 1 to 10, so if I delete the item 7, I want that the numbers will be renumbered. hope you can help me guys
Image Analyst
Image Analyst el 14 de Jul. de 2014
You're going to have to use something like sscanf() to parse the number out of the line of text. Basically strip off the numbers and rebuild your list from scratch with new numbers.

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 14 de Dic. de 2011

Comentada:

el 14 de Jul. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by