How can I sort(delete) contents in listbox?

3 visualizaciones (últimos 30 días)
Haksun Lee
Haksun Lee el 7 de Jun. de 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);

Respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by