How can I sort(delete) contents in listbox?

2 Ansichten (letzte 30 Tage)
Haksun Lee
Haksun Lee am 7 Jun. 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);

Antworten (1)

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by