MATLAB GUIDE gui 列表框间歇性消失,并出现看似过时的错误

发布于 2024-09-14 04:11:24 字数 1526 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看海 2024-09-21 04:11:24

这是一个已知的编程错误,与竞争条件无关!

它应该是这样工作的:

对于弹出窗口和单选列表,string属性必须不为空,即有一些内容。但默认情况下它是空的,因此必须始终定义它。

在弹出窗口中显示的(在列表中突出显示的)项目由两个属性string(作为字符串元胞数组)和value(即1)定义默认)。

获取空数组中的第一个元素显然不起作用,因此无法呈现控件!

您的列表框控件是单选的 - 其属性 min min min min min min min maxvalue 是标量且 >0。如果属性 min > ,列表框(但不是弹出窗口)可以进行多项选择。 max,在这种情况下,value 可以是一个数组(这也意味着空),空的string 不会导致问题。

阅读 MATLAB 帮助以了解 uicontrol 属性 string、value、min、max、listboxtop

在实践中

  • 您说它在使用 GUIDE 编辑后会显现出来。 GUIDE 创建代码隐藏。编辑后,旧的代码隐藏有时仍保留在原处。
  • 有时 MATLAB 对一条语句内的顺序很敏感,即 set(hlist, 'value', 2, 'string', {'aa','bb'}) 简单地设置 value< /code> 到 2 之前 string 足够长并且使 uicontrol 无效。
  • MATLAB 是有 bug 的,每个新版本每年两次都会删除旧版本并带来新的 bug。如果您确定调试模式无法正常工作,则需要 MATLAB 支持。我运行与 m 代码、p 代码和编译的 exe 相同的代码 - 相同的代码的行为不同,主要是 GUI 的工作方式不同。

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) and value (which is 1 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 and value is scalar and >0. Listbox (but not Popup) can be multi-selection if property min > max, in this case value can be an array (which implies also empty) and empty string will not cause problems.

Read MATLAB Help for uicontrol properties string, value, min, max, listboxtop

In praxis

  • You say it manifests itself after editing with GUIDE. GUIDE creates code-behind. After editing old code-behind sometimes remains in place.
  • Sometimes MATLAB is sensitive to the order inside one statement, i.e. set(hlist, 'value', 2, 'string', {'aa','bb'}) sets naively value to 2 before string is long enough and invalidates uicontrol.
  • MATLAB is buggy, every new version twice a year removes old and brings new bugs. If you are sure that debugging mode works not as it should then it is a case for MATLAB support. I have same code running as m-code, p-code and compiled exe - same code behaves differently, mainly in how GUI works.
丘比特射中我 2024-09-21 04:11:24

根据我的经验,当 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 the value property - set it to 1 for safety reasons.

Other than that, check the excellent comments by @Mikhail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文