设置命令有两种设置属性值的可能性

发布于 2024-10-27 16:07:34 字数 306 浏览 2 评论 0原文

对于 matlab 中的某些 hobject,set(hobject, 'enable', 'inactive') 命令可以正常工作。
对于其他的,比如工具栏按钮,只有set(hobject, 'enable', 'off')

我是否有一组 listObjects
是否有类似 set(listObjects, 'enable', ['inactive'|'off']) 的内容,其中我如果属性有效,则将其设置为“非活动”;如果属性无效,则将其设置为“关闭”?

For some hobjects in matlab, the set(hobject, 'enable', 'inactive') command will work fine.
for others, like toolbar buttons, there's only set(hobject, 'enable', 'off').

Is I have a set of listObjects,
is there something like set(listObjects, 'enable', ['inactive'|'off']) in which I set the property to 'inactive' if it's valid, and 'off' if it's not a valid property?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

乖乖兔^ω^ 2024-11-03 16:07:34

如果您只需要检查两个值,则可以使用 try/catch 块。

try
    set(hobject,'enable','inactive');
catch exception
    if strcmp(exception.identifier,'MATLAB:hg:propswch:FindObjFailed')
        set(hobject,'enable','off');
    else
        throw(exception)
    end
end

这样,它会在您第一次尝试设置该值时检查该值是否可接受。如果没有,它会尝试下一个替代方案。如果错误是由于其他原因造成的(例如,您输入了无效的属性名称),则会将错误抛出到屏幕上。

顺便说一句,当我尝试设置属性不接受的值时,我在计算机上遇到了错误标识符:MATLAB:hg:propswch:FindObjFailed。尽管我怀疑它应该是相同的,但您可能想看看您在计算机上得到了什么并在 strcmp 函数中使用它。

If you have only two values that you need to check, you can use a try/catch block.

try
    set(hobject,'enable','inactive');
catch exception
    if strcmp(exception.identifier,'MATLAB:hg:propswch:FindObjFailed')
        set(hobject,'enable','off');
    else
        throw(exception)
    end
end

This way it checks the first time you try to set the value, if it is acceptable. If not, it tries the next alternative. If the error is due to something else (for e.g., you entered an invalid property name), it throws the error to the screen.

BTW, the error identifier: MATLAB:hg:propswch:FindObjFailed was what I got on my machine when I tried to set a value that the property wouldn't accept. Although I suspect it should be the same, you might want to see what you get on your machine and use that in the strcmp function.

兔姬 2024-11-03 16:07:34

您可以首先通过读取对象的 'enable' 属性来检查对象的“类型”。

enableStatus = get(listOfObjects,'enable');

%# identify who is active and who is on
activeObjects = listOfObjects(strcmp(enableStatus,'active'));
onObjects = listOfObjects(strcmp(enableStatus,'on'));

%# set proper status
set(activeObjects,'enable','inactive')
set(onObjects,'enable','off')

You can check for the 'type' of objects by reading their 'enable' property first.

enableStatus = get(listOfObjects,'enable');

%# identify who is active and who is on
activeObjects = listOfObjects(strcmp(enableStatus,'active'));
onObjects = listOfObjects(strcmp(enableStatus,'on'));

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