MATLAB GUIDE gui 列表框间歇性消失,并出现看似过时的错误
我正在使用 GUIDE 构建一个简单的 MATLAB gui。我有一个项目列表框。大多数时候,它会按预期工作,但有时(通常在我使用 GUIDE 编辑图形之后)填充列表框会导致它消失,并显示以下消息:
Warning: single-selection listbox control requires a scalar Value
Control will not be rendered until all of its parameter values are valid
此行为无法调试!当我单步执行时,它按预期工作(我怀疑这是一种线程竞赛或其他什么)。此外,在相同的条件下,它通常会在重新启动 MATLAB 环境后消失。
有关此错误的所有文档均引用以前/旧版本的 MATLAB(我使用的是 R2010a)。
有关此主题的任何想法或信息将不胜感激!
编辑:感谢米哈伊尔,我似乎已经解决了这个问题。我在这里发布我的代码以供将来参考。
经过大量的调试打印和疯狂点击后,我发现有时当您询问列表框选择了什么时,您会得到一个空结果。这个问题和其他问题让事情变得一团糟。我将所有与列表框的书写交互转移到一个集中函数中,并编写了一些测试代码以确保事情保持应有的方式。
请注意,这已在我自己的环境(R2010a)中进行了测试,但并未进行广泛测试。另外,代码有点多余,但无论如何让我感觉很好。 (即。itemcount
不能小于 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个已知的编程错误,与竞争条件无关!
它应该是这样工作的:
对于弹出窗口和单选列表,
string
属性必须不为空,即有一些内容。但默认情况下它是空的,因此必须始终定义它。在弹出窗口中显示的(在列表中突出显示的)项目由两个属性
string
(作为字符串元胞数组)和value
(即1
)定义默认)。获取空数组中的第一个元素显然不起作用,因此无法呈现控件!
您的列表框控件是单选的 - 其属性
min
min
min
min
min
min
min
max
和value
是标量且 >0。如果属性min
> ,列表框(但不是弹出窗口)可以进行多项选择。max
,在这种情况下,value
可以是一个数组(这也意味着空),空的string
不会导致问题。阅读 MATLAB 帮助以了解 uicontrol 属性
string、value、min、max、listboxtop
在实践中
set(hlist, 'value', 2, 'string', {'aa','bb'})
简单地设置value< /code> 到
2
之前string
足够长并且使 uicontrol 无效。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
string
property 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 is1
by 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
<max
andvalue
is scalar and >0. Listbox (but not Popup) can be multi-selection if propertymin
>max
, in this casevalue
can be an array (which implies also empty) and emptystring
will not cause problems.Read MATLAB Help for uicontrol properties
string, value, min, max, listboxtop
In praxis
set(hlist, 'value', 2, 'string', {'aa','bb'})
sets naivelyvalue
to2
beforestring
is long enough and invalidates uicontrol.根据我的经验,当
value
属性大于列表框中的条目数时,最常发生此错误。因此,每当您重新填充列表框时,您都应该更新value
属性 - 出于安全原因将其设置为 1。除此之外,请查看 @米哈伊尔。
In my experience, this error most often occurs when the
value
property is larger than the number of entries in the listbox. Thus, whenever you repopulate the listbox, you should update thevalue
property - set it to 1 for safety reasons.Other than that, check the excellent comments by @Mikhail.