List box dissapearing when removing items from it.

1 view (last 30 days)
[EDIT: 20110616 14:29 CDT - reformat - WDR]
I am using this bit of code to remove items from a listbox. however if i remove all of the items and then try and add another the list box itself dissapears.s I get this warning:
Warning: multi-selection listbox control requires that Value be an integer within String range
Control will not be rendered until all of its parameter values are valid.
The code is as follows. I think I it not allowing indices to be zero. but I don't know what to put in the code to prevent the dissapearing of the listbox.
Here is the code:
____________________________________________________________
% --- Executes on button press in remove_button.
function remove_button_Callback(hObject, eventdata, handles)
% hObject handle to remove_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%selected item reorganizes the listbox index
selected = get(handles.listbox2,'Value');
prev_str = get(handles.listbox2, 'String');
len = length(prev_str);
if len > 0
if selected(1) == 1
indices = [(selected(end)+1):length(prev_str)];
else
indices = [1:(selected(1)-1) (selected(end)+1):length(prev_str)];
end
% elseif len == 0
% set(handles.listbox2, 'Value',1);
prev_str = prev_str(indices);
set(handles.listbox2, 'String', prev_str, 'Value', min(selected, length(prev_str)));
end
__________________________________________________________
How do I go about keeping the list box in the GUI when nothing is in it?
Thanks
Bill
  1 Comment
Fangjun Jiang
Fangjun Jiang on 16 Jun 2011
When you try to remove one item from the list box, you could use this syntax: Prev_str(selected)=[].

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 16 Jun 2011
I've done similar things, having a "Remove" button, if clicked, it removes the highlighted item in the list box. Here is my code for your reference. I remember that I have to take care of the synchronization of the 'string' and 'value' property of the list box. Also, you need to take care when the list box is empty.
contents = get(handles.lbDataItemSelected,'String');
if length(contents)<1; return; end; %if already empty, do nothing
Index=get(handles.lbDataItemSelected,'Value');
contents(Index)=[]; %remove the item
Value=Index-1;
if Value<1; Value=1;end %take care of exception
set(handles.lbDataItemSelected,'String',contents,'Value',Value);

More Answers (2)

Walter Roberson
Walter Roberson on 16 Jun 2011
Which MATLAB version are you using? I do not see anything obviously wrong at the moment, though your min() looks a bit dubious and your setting of indices looks over-complicated
As a note of code optimization:
if len > 0
indices = setdiff(1:length(prev_str), selected);
prev_str = prev_str(indices);
set(handles.listbox2, 'String', prev_str, 'Value', []);
end
This is based upon the assumption that you want to delete all of the selected items and leave everything else unselected. If you only want to delete the first selected item and leave any other selected items as selected, you would use
if len > 0
indices = setdiff(1:length(prev_str), selected(1));
prev_str = prev_str(indices);
set(handles.listbox2, 'String', prev_str, 'Value', indices);
end
[EDIT]
Opps, that last code has a bug in created in the new indicates. I will need to figure out a nice way to do it.
  2 Comments
William
William on 16 Jun 2011
We use 2007b here. Thank you so much for your help. I am trying to create a .MAT to CVS GUI and it's killing me.
Bill
Walter Roberson
Walter Roberson on 16 Jun 2011
The item to be deleted is based upon what is selected? Only the first one selected or all the ones that are selected? Which items should be selected afterwards?

Sign in to comment.


William
William on 16 Jun 2011
Thanks much!

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!