Question about getpts.m code

4 次查看(过去 30 天)
John
John 2014-9-27
编辑: Guillaume 2014-9-27
I was looking at the source code for the function getpts.m, which in 2014a is located in $MATLABROOT\toolbox\images\images\getpts.m.
I understand everything about the function code except lines 33:38. I don't want to paste the source here because the source suggests it's copyrighted. But those lines will cause getpts called in this fashion: getpts(<a string of any MATLAB function>), to execute that function and return from the function. I cannot think of a reason why that piece of code would be there. Is there some meaningful and esoteric reason for the presence of that seemingly useless chunk of code? I would really love to know!

回答(1 个)

Guillaume
Guillaume 2014-9-27
编辑:Guillaume 2014-9-27
As the comment says (in 2013b), it's to deal with callbacks (that getpts configured).
If you look further down (it's on line 74 in 2013b), getpts sets up callback methods that call getpts with a string. If you look even further down (line 177 onwards), you have functions with the same names as the strings in the callbacks. The whole thing is just to dispatch the callback to those functions.
The reason the callbacks are not set up to call these functions directly is because these functions are only visible from within getpts (since they're defined in that m file and are not the main function).
You could achieve the same thing with anonymous functions, which would have been cleaner, e.g:
set(fig, 'KeyPressFcn', @(o,e) privatefcn);

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by