Interaction between two list box problem

2 views (last 30 days)
I have two list boxes, one with some data and the other empty. I have two buttons, one to add data from one to the other and the other does the same but in the opposite direction. When the data gets moved across it gets removed from the original list box.
numm=get(handles.listbox1,'Value')
list1=get(handles.listbox1,'String');
list2=get(handles.listbox2,'String');
set(handles.listbox2,'String',[list2;list1(numm)]);
list1(numm,:) = []
set(handles.listbox1,'String',list1);
For example if my listbox had 4 lines of data in it, I can select any of the first three and move it over, but if I select the 4th bit of data first and move it across I get the error
"Warning: single-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 "
I'm not sure why this is happening.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 14 Oct 2011
When you eliminate one item in the string of listbox1 and update the string of listbox1, you didn't update the 'value' of listbox1.
For example, at the beginning, listbox1 has 4 items, the 4th item is selected. After you remove the 4th item, there are 3 items in listbox1, but the 'value' is still 4.
To make your program robust, consider and test out the following:
  1. What if listbox1 and/or listbox2 is empty?
  2. What if listbox1 and/or listbox2 becomes empty?
  3. What if listbox1 and/or listbox2 contains duplicated item?
Function isempty() and unique() can help.
  1 Comment
Paul
Paul on 15 Oct 2011
Ah of course, as the value was still set at 4 it was trying to select the 4th element while there was only 3 left. Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on App Building 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!