What does the string in 'DeleteFcn' evaluate to?
Ältere Kommentare anzeigen
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 Kommentare
Adam
am 6 Jun. 2018
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....
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Entering Commands finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!