Thomas - perhaps the condition
~strcmpi(Items_2, Item_1)
is never evaluating to true and so you never enter the body of the while loop. Part of the problem may be because Items_2 is always initialized to an empty cell array (is this intentional?) and is never set to what is currently in your list. Note that if I try the above, with
g = {};
strcmpi(g, 'hello')
then an empty array is returned...so I suspect that is the real problem. The condition is never satisfied. Try doing something like the following instead
function pushbutton2_Callback(hObject, eventdata, handles)
selectedString = handles.selectedItemStr;
listBox2Data = get(handles.listbox2,'String');
if isempty(listBox2Data) || ~any(strcmpi(listBox2Data, selectedString))
listBox2Data = [selectedString, listBox2Data];
set(handles.listbox2,'String',listBox2Data);
end
No need for a while loop in this case. Note that we use the call to any since the strcmpi may return an array of 0 or 1s for each element in the list box. any will return true if at least one of these elements is a one (indicating that the selected string is already in the list). If the call returns 0 (i.e. no element in list matches the selected string) then we add the item to the list.
Best Answer