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