function handle and Subfunction in the command window

3 views (last 30 days)
Hello,
I am working on this quiz question and I don't really understand how this code is working. I have the code attached as an image.
Starting with V2, I see it is myfun2(10,11). I already see that myfun2 is not defined in this folder, so I rule it to be undefined and thus an error.
For V1, start with myfun1(10,11). Then see that V is the LOCAL_myfun2(10,11), with is a+b = 21.
I don't really understand how the H = @LOCAl_myfun2 is working.
Thanks for any clarification of subfunctions and function handles. Also, how does this question depend on whether its run in myfun1.m file, or the command window?

Accepted Answer

Henric Rydén
Henric Rydén on 26 Jun 2014
Hi,
the file myfun1.m contains two functions, one is the main function myfun1 , the other is a local function and can normally not be accessed from outside the main function. To emphasize this, they've named it LOCAL_myfun2 .
Now, they've made it possible to extract LOCAL_myfun2 from myfun1 using the second output from myfun1 . Function handles are simply variables that are functions. After the first command
[V1, myfun2] = myfun1(10,11)
you have one scalar, V1 = 21, and you've also extracted LOCAL_myfun2 in a function handle and named it myfun2 . In the next line, you are using that function and passing 10 and 11 to it, resulting in V2 = 21 .
Hope this helps

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!