How can I sort(delete) contents in listbox?

3 vues (au cours des 30 derniers jours)
Haksun Lee
Haksun Lee le 7 Juin 2012
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);

Réponses (1)

Titus Edelhofer
Titus Edelhofer le 7 Juin 2012
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 commentaires
Haksun Lee
Haksun Lee le 7 Juin 2012
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 le 8 Juin 2012
No. Each function is a separate function with a separate set of variables (workspace).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by