Scope of variables passed to function handles?
1 次查看(过去 30 天)
显示 更早的评论
Hi, everyone,
I want to ask about the non-input variables in function handles.
For example, following code returns h(1)=2.
b=1;
h=@(a)a+b;
clear b;
display(sprintf('h(1)=%d', h(1)));
Is this kind of usage safe and correct?
Dehuan
0 个评论
采纳的回答
matt dash
2014-12-16
As far as i know, yes this is safe and correct. Any non-input variables pass their values to the function at the time the function handle was created. Subsequent changes to or clearing of those variables has no effect on the function.
2 个评论
Adam
2014-12-16
Yes, this is correct. It is also why passing the handles structure, for example, in a GUIDE-based file to a callback is not a good idea, because a copy of the structure at the exact time the callback is set is hard-coded into the callback, irrespective of when it is called.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!