채택된 답변

Image Analyst
Image Analyst 2011년 12월 14일

4 개 추천

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

Walter Roberson
Walter Roberson 2011년 12월 14일
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.

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

추가 답변 (1개)

Maryam Emad
Maryam Emad 2011년 12월 15일

0 개 추천

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

Image Analyst
Image Analyst 2011년 12월 16일
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 2011년 12월 19일
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 2011년 12월 19일
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 2011년 12월 19일
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 2011년 12월 19일
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 2011년 12월 19일
Thaaaaanx alot Walter .. it is okaaay now ^_^
thanx thanx Image Analyst : )
Jihad Chamseddine
Jihad Chamseddine 2014년 7월 14일
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 2014년 7월 14일
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.

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

태그

질문:

2011년 12월 14일

댓글:

2014년 7월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by