フィルターのクリア

How can I sort(delete) contents in listbox?

3 ビュー (過去 30 日間)
Haksun Lee
Haksun Lee 2012 年 6 月 7 日
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 Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by