How can I sort(delete) contents in listbox?

1 次查看(过去 30 天)
Hello! I use listbox, and can add items to listbox and have to delete items from listbox. In case of adding, it works properly, but in case of delete & sort, it doesn't work ..! What's the problem with the code below?
-----------------------------------------------------------------------------------
%adding items to listbox
function pushbutton3_Callback(hObject, eventdata, handles)
oldList = get(handles.listbox1,'string');
newVal = PacketContent.title;
newList = [oldList; newVal];
%sort item
function pushbutton5_Callback(hObject, eventdata, handles)
newList_sorted=sort(newList);
set(handles.listbox1,'string',newList_sorted);

回答(1 个)

Titus Edelhofer
Titus Edelhofer 2012-6-7
Hi Haksun,
in your pushbutton5_Callback you need to define newList, e.g.
newList = get(handles.listbox1, 'string');
before doing the sort.
Edit: And what about delete? Pretty much the same
newList = get(handles.listbox1, 'string');
% e.g. remove Nr. 2
newList(2) = [];
set(handles.listbox1, 'string', newList, 'value', 1);
Note: you should always set the value to e.g. 1, because if you delete the last of newList, the value will be length(newList)+1, i.e., out of range (you will see this by a warning and a disappearing listbox).
Titus
  2 个评论
Haksun Lee
Haksun Lee 2012-6-7
Oh, thanks Titus
I thought, once I declare newList at button3's callback, it also works at button5's callback.
And then, how about delete items from listbox? I can't find out proper solution T.T
I will look forward your answer.
Haksun
Titus Edelhofer
Titus Edelhofer 2012-6-8
No. Each function is a separate function with a separate set of variables (workspace).

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by