In
Matlab, a. m file can have more than one sub-function, but only one main function, and the M file name must be the same as the main function
in a single m document there are usually two ways to define a child function:
1. Nesting Definitions
myfunc1 will share the variable name with the main function. In this case, it would be inappropriate to use the same variable name, if
not intentionally. function MainFunc (
...
A = Myfunc1 ();
...
function myfunc1 ()
...
End
End
2. Non-nested definitions
function MainFunc ()
...
A = Myfunc1 ();
...
End
function myfunc1 ()
...
End
In The second case, the MYFUNC1 and the main function are two separate functions, and write them in two separate files
There is no difference in. Variable name how to get up, there will be no conflict.
To illustrate:
%%-----------------------------------------------------------------------function Usemyfunarray ()%The main function must be at the top%examples of sub-functions Max1=find_max (1, -,3) Max2=find_max (7,3,9) function Max=find_max (a,b,c)%Child Functionsif(a>=b) & (a>=c) Max=A;elseif (b>=a) & (b>=c) Max=b;ElseMax=C;end%%-----------------------------------------------------------------------
Methods for defining sub-functions in the same file in MATLAB