MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何检查MATLAB句柄是否有效? (https://www.labfans.com/bbs/showthread.php?t=23011)

poster 2019-12-10 16:49

如何检查MATLAB句柄是否有效?
 
我使用它的句柄处理图上的数据:

x = 1:10; y = sin(x); h1 = line(x,y); 但是,如果在脚本实际终止之前关闭图形,则执行类似的操作将返回错误。

>>set(h1,'Color','green') % line is green ??? Error using ==> set Invalid handle object. 有没有办法在对h1进行任何操作之前检查它是否有效?


回答:
您可以使用[URL="https://www.mathworks.com/help/matlab/ref/ishandle.html"]ishandle[/URL]函数首先检查图形手柄是否有效:

if ishandle(h1) set(h1, 'Color', 'green'); end [B]更新:[/B]

对于较新版本的MATLAB,句柄对象是实际对象,而不仅仅是数字值。更好的选择是对句柄对象使用[URL="https://www.mathworks.com/help/matlab/ref/handle.isvalid.html"]isvalid[/URL]方法:

if isvalid(h1) set(h1, 'Color', 'green'); end

[url=https://stackoverflow.com/questions/1956626]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 23:33

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.