Updating listbox when deleting items

3 Ansichten (letzte 30 Tage)
Vincent MAILLET
Vincent MAILLET am 9 Nov. 2017
Kommentiert: Jan am 11 Nov. 2017
Hi,
I am currently creating a GUI with GUIDE on MATLAB 2010 and I have a rather strange issue. I have two listbox (listbox1 and listbox2) and when pressing "enter" when focus on listbox1, said item appears in listbox2 (cell array and use of "unique" to avoid duplicate). listbox1 is not altered. Then, by pressing "enter" with focus on listbox2, I delete said item from listbox2. So, being a newbie, I had at first the notorious "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 "
So I add some code to set Value at 1.
And then it became strange. The code works perfectly fine when I am doing it step by step on the debug tool, but as soon as I go back to normal, the warning re-appears.
Here is my code:
if strcmp(eventdata.Key, 'return')
handles.exp_names = handles.exp_names(~strcmp(handles.exp_names,handles.exp_deletion));
set(handles.listbox2,'Value',1);
refreshdata(handles.listbox2);
set(handles.listbox2,'String',handles.exp_names);
set(handles.listbox1,'visible','on')
guidata(hObject,handles);
end
Any idea of why this happens ?
Thank you
Vincent

Akzeptierte Antwort

Jan
Jan am 9 Nov. 2017
The error message occurs, if the Value is not inside the range [1:numel(handles.exp_names)]. If you delete the last string from the list box, setting the value to 1 is an error.
I do not know, why this works in debug mode and fails without it. Perhaps there is another listbox in the figure, which has an invalid Value?
  4 Kommentare
Vincent MAILLET
Vincent MAILLET am 10 Nov. 2017
HI again,
I am positive about the warning coming from listbox2. The value of the selection in the listbox is always a number between 1 and length(string). But working with the debugger gave me an idea. I inserted a pause between two lines:
handles.exp_names = handles.exp_names(~strcmp(handles.exp_names,handles.exp_deletion));
pause(1)
set(handles.listbox2,'Value',v);
set(handles.listbox2,'String',handles.exp_names);
guidata(hObject,handles);
And suddenly everything work fine. No more warning whatever I do. Note that the time delay is not really important. It works with 0.0001s.
Might not be elegant but it is the only answer I got for now.
Jan
Jan am 11 Nov. 2017
A pause can in fact solve a problem here, because it triggers the update of the GUI elements. The the position after the change of handles.exp_names does not really matter. All you must ensure, is that the display is not updated by pause, drawnow or a stop in the debugger, as long as 'String' and 'Value' are not corresponding. Therefore I'd prefer to update both properties in the same command:
set(handles.listbox2, 'Value', v, ...
'String', handles.exp_names);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by