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

 Réponse acceptée

Jan
Jan le 9 Nov 2017

0 votes

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 commentaires

Vincent MAILLET
Vincent MAILLET le 10 Nov 2017
Sorry, I was not clear enough. The issue does not happens on the last one as in the only left, but the last one as the one at the end of the listbox (might be 2 or 20 elements left in the listbox). I dealt with the issue of value=1 for only one element left. Oddly enough, when there is only one element left in the listbox (so it is the last in both senses), nor the error nor the warning appears. So I am completely lost right now.
Thank you for your response.
Vincent
Jan
Jan le 10 Nov 2017
Modifié(e) : Jan le 10 Nov 2017
Start with finding the listbox, which causes the problem. Perhaps it is not the one you expect. Then check the code, which changes the Value or the number or strings. Use the debugger to examine, what happens. Adding a manual check might be useful also:
v = get(ListBoxH, 'Value');
if v < 1 || v > numel(get(ListBoxH, 'String'))
error('Here is the problem!');
end
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.
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);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by