matlab - What is a function handle and how is it useful? -
Can someone tell me the meaning of the "code> @ (function handle) operator and why use it In MATLAB, essentially serves as an indicator for a specific example of a function.
Some of the other answers have discussed something in its experiments, but I have another use here I will add that I have often for this: to maintain access to those tasks which are no longer "in the field".
For example, the following function initiates a value of Count , and then a function nested functions handle increments :
function fHandle = start_counting (count) vs (calculation); FHandle = @waste; Work increment salary = count + 1; Disp (calculation); The end function is increment , it can only be done within the start_counting function (i.e. Start_counting its "scope"). However, by returning a handle to the increment function, I can still use it outside of start_counting , and it still works in the workspace of start_counting Maintains access to variables ! This allows me to do this:
& gt; & Gt; Fh = start_counting (3); Count% 3 and return handle 3 & gt; & Gt; FH (); % # Your handle 4 and gt; & Gt; FH (); 5 Note how we can increase the counting even if we are outside of the function start_counting but you can again Calling start_counting can make it more interesting and handle functions in another variable:
& gt; & Gt; Fh2 = start_counting (-4); -4 & gt; & Gt; FH2 (); -3 & gt; & Gt; FH2 (); -2 & gt; & Gt; FH (); To increase% # 6, first call the hand & gt; & Gt; FH2 (); To increase the% # again, call the second handle -1 Note that these two separate counters are operated independently fh And indicates various examples of wage increments with the increments functional workspace with different values for count . .
In addition to the above, using the function in conjunction with nested functions can also help in enabling GUI designs, as I have described.
Comments
Post a Comment