App Designer: How to delete items from listbox?

Hello everybody,
I want to make it possible to delete the selected item of a listbox by pushing a button. I'm using App Designer.
With the following code I'm getting the error message "Matrix index is out of range for deletion.". But if the first item is selected, nothing happens at all.
% Button pushed function: deleteButton
function deleteButtonPushed(app, event)
hListBox = app.ListBox;
currentItems = get(hListBox);
[~, index] = ismember(hListBox.Value, hListBox.Items);
newItems = currentItems;
newItems(index) = [];
set(hListBox,newItems);
end
So, I guess that's not the way to delete. So I tried to replace the selected item by another one. Just to test if it's possible to change anything in the listbox
newItems(index) = newItems(end);
set(hListBox,newItems);
Like this, I get the following message:
"Error using matlab.ui.control.ListBox/set You cannot set the read-only property 'BeingDeleted' of ListBox."
Can somebody help me? After trying and googling for hours, I'm not even sure if it's possible to change anything in the listbox at runtime.
(Matlab R2018b)
Thank you!!

 Réponse acceptée

Luna
Luna le 12 Jan 2019
Modifié(e) : Luna le 12 Jan 2019
Hi Marie,
Below code will delete your corresponding item.
function ButtonPushed(app, event)
[~,idx] = ismember(app.ListBox.Value,app.ListBox.Items);
app.ListBox.Items(idx) = [];
end
The below part in your code currentItems will give you the properties of the list box not the Item List in the listbox.
That's how get method works. Get method, gets the properties of the object you are referring in a struct. So that's why you see an error, you can see but you can't set all of the properties of an object because some of them might be protected.
hListBox = app.ListBox;
currentItems = get(hListBox);
When you debug your code(put red dot inside the pushbutton callback and evaluate the line currentItems = get(hListBox) with one step forward), you will see currentItems will give you the below properties:
currentItems =
struct with fields:
Multiselect: 'off'
Value: 'Item 1'
Items: {'Item 1' 'Item 3' 'Item 4'}
ItemsData: []
ValueChangedFcn: [function_handle]
Parent: [1×1 Figure]
HandleVisibility: 'on'
BusyAction: 'queue'
BeingDeleted: 'off'
Interruptible: 'on'
CreateFcn: ''
DeleteFcn: ''
Type: 'uilistbox'
Tag: ''
UserData: []
Enable: 'on'
Visible: 'on'
FontName: 'Helvetica'
FontSize: 12
FontWeight: 'normal'
FontAngle: 'normal'
FontColor: [0 0 0]
BackgroundColor: [1 1 1]
InnerPosition: [147 333 100 74]
Position: [147 333 100 74]
OuterPosition: [147 333 100 74]

8 commentaires

Marie Eckert
Marie Eckert le 12 Jan 2019
Thank you so much, it works perfectly!
I'm very new to matlab, so your explanation also helped me a lot!
Luna
Luna le 14 Jan 2019
Your welcome :)
Sorry i'm having trouble with this code,
I used to delete items in the list box but appears this error
"Error using cell/ismember
Input A of class double and input B of class cell must be cell arrays of character vectors, unless one is a character vector."
I don't know how to solve this
try this:
[~,idx] = ismember({num2str(app.ListBox.Value)},app.ListBox.Items);
or put a debug point on this line and see the type of app.ListBox.Value and app.ListBox.Items. Then look at ismember function inputs by typing "doc ismember" in command window.
KAI LUO
KAI LUO le 11 Nov 2019
This is to delete the line you specified, if I want to delete the line that is empty, what should I do?
You can check it with isempty function. It gives logical 1 if the array,cell array or struct, etc. is empty. All you will do is:
if isempty(something)
% delete someother thing
else
% do nothing
end
Ricky Zhang
Ricky Zhang le 1 Fév 2021
Modifié(e) : Ricky Zhang le 1 Fév 2021
careful: seems it doesn't work when ItemsData is not empty. app.ListBox.Value will return the corresponding item's data instead.
Assuming your cell array A is below:
A = {'Item1',[],2,3,'','Item2'};
Then you have to use cellfun to detect each element isempty or not.
empty_idx = cellfun(@isempty, A)
Basically empty_idx will be as follows:
% empty_idx =
% 1×6 logical array
% 0 1 0 0 1 0
% >>
First you should have check all the Items and their data types inside your item list array (most probably a cell or can be categorical array you can check it in the documentation ) you defined for the ListBox.

Connectez-vous pour commenter.

Plus de réponses (1)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by