What does the string in 'DeleteFcn' evaluate to?
显示 更早的评论
I need to debug a piece of code and couldn't understand what the string in 'DeleteFcn' evaluate to. Also, what would the be the output of DeleteFcn attribute here
if true
c = text('Parent',b, ...
'DeleteFcn','eval(''delete(get(gcbo,''''userdata''''))'','''')', ...
'HandleVisibility','off', ...
'Tag','ColorbarDeleteProxy', ...
'UserData',66.0018, ...
'Visible','off');
end
2 个评论
Adam
2018-6-6
It would appear to delete the 'UserData' field of whatever handle's callback it is that triggers it, which I guess would be the figure, though I never use either eval or gcbo myself and rarely 'UserData'.
'DeleteFcn','eval(''delete(get(gcbo,''''userdata''''))'','''')', ...
Ugh. Badly written code. Do NOT learn from this code. So many levels of bad in just one line. Ugh.
- The MATLAB documentation specifically advises against a defining a callback as a string: "Defining a callback as a character vector is not recommended. The use of a function specified as function handle enables MATLAB to provide important information to your callback function." As the documentation recommends, you should use function handles for defining callbacks.
- Calling gcbo is superfluous, because when using a function handle the first input argument is a handle to that object.
- eval is a chainsaw that some beginners overuse, perhaps because they underestimate the damage that it can cause. The MATLAB documentation has an entire page which advises why evaluating strings should be avoided. Read this to know more:
'UserData',66.0018, ...
4. Hardcoded graphics handles!? This code just gets worse and worse....
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!