I am building a straightforward MATLAB gui using GUIDE. I have a listbox of items. Most of the time, it works as expected, but sometimes (usually after I edit the figure with GUIDE) populating the listbox causes it to disappear, along with this message:
Warning: single-selection listbox control requires a scalar Value
Control will not be rendered until all of its parameter values are valid
This behavior defies debugging! When I step through, it works as expected (I suspect it is a kind of thread race or something). Furthermore, it usually goes away after restarting the MATLAB environment, under identical conditions.
All documentation found on this error refer to previous/ancient versions of MATLAB (I am using R2010a).
Any ideas or information on this subject would be greatly appreciated!
EDIT: thanks to Mikhail, I seem to have solved the problem. I am posting my code here for future reference.
After lots of debug printing and wild clicking, I found that sometimes when you ask the listbox what is selected, you get an empty result. This and other problems made things go haywire. I moved all of my writing interactions to the listbox into a centralized function, and I wrote some testing code to ensure that things stay the way they should.
Please note that this has been tested in my own environment (on R2010a) and not extensively. Also, the code is a bit redundant, but it made me feel good anyway. (ie. itemcount can't be less than 0 ...)
function ensure_listbox_ok(handles)
%check to make sure it does not suck - ask what it has
thestrings = get(handles.listbox_files, 'String');
selection = get(handles.listbox_files, 'Value');
itemcount = length(thestrings);
betterselection = selection;
if(itemcount <= 0)
betterselection = 1;
else
if(selection > itemcount)
betterselection = itemcount;
end
end
%never use zero!!!! even if 1 is out of bounds.
if(isempty(betterselection) || betterselection <= 0)
betterselection = 1;
end
%uncomment for debug logging
%display(['Was: ' num2str(selection) ', cleaned: ' num2str(betterselection)]);
%update if we are out of bounds.
if(isempty(selection) || betterselection ~= selection)
set(handles.listbox_files, 'Value', betterselection);
end
This is a known programming-error and it has nothing to do with race condition!
This is how it should work:
For Popup and single-selection List
stringproperty must be not-empty, i.e. have some content. But it is empty by default therefore it must be always defined.In Popup displayed (in List highlighted) item is defined by two properties
string(as cell array of strings) andvalue(which is1by default).Taking first element in an empty array obviously does not work, therefore the control can not be rendered!
Your Listbox control is single-selection - its properties
min<maxandvalueis scalar and >0. Listbox (but not Popup) can be multi-selection if propertymin>max, in this casevaluecan be an array (which implies also empty) and emptystringwill not cause problems.Read MATLAB Help for uicontrol properties
string, value, min, max, listboxtopIn praxis
set(hlist, 'value', 2, 'string', {'aa','bb'})sets naivelyvalueto2beforestringis long enough and invalidates uicontrol.