How to nest a function within the pushbutton callback function?

18 views (last 30 days)
Screen Shot 2019-10-02 at 3.23.06 AM.png
  1 Comment
Stephen23
Stephen23 on 2 Oct 2019
"How to nest a function within the pushbutton callback function?"
The best answer is to forget about bloated GUIDE and write your own GUI code, then you can use nested functions for your callbacks (as well as gaining simpler, more efficent code, over which you have much more control).

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 2 Oct 2019
You appear to be using GUIDE. Nesting functions is not recommended with GUIDE. In order to nest functions, every function in the same .m file must have an "end" statement that matches the "function" statement. GUIDE does not know how to edit files that have "end" matching "function", and will not be able to work with the .m file once the "end" are put in.
Your code assigns y only within function fnc . Outside of fnc then y is unreachable: it would go out of scope as soon as the call to fnc returned. You would have to call fnc with an appropriate parameter, and you would probably assign the result of the call to fnc to a variable. With fnc being a nested function inside pushbutton1_Callback, the call to fnc would have to be within pushbutton1_Callback .
Your function fnc takes a single parameter that you have named ErrDrive . It is not obvious at the moment where you are going to find data to pass as that parameter; perhaps an appropriate value is stored in the handles structure ?

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!