Expert Answer:
s:
What you need to do in general is get function handles to your subfunctions from within the primary function and pass them outside the function where you can unit test them. One way to do this is to modify your primary function such that, given a particular set of input arguments (i.e. no inputs, some flag value for an argument, etc.), it will return the function handles you need.
For example, you can add a few lines of code to the beginning of your function so that it returns all of the subfunction handles when no input is specified:
function things = myfunc(data)
if nargin == 0 % If data is not specified...
things = {@mysubfunc @myothersubfunc}; % Return a cell array of
% function handles
return % Return from the function
end
% The normal processing for myfunc...
stuff = mysubfunc(data);
things = mean(stuff);
end
function mysubfunc
% One subfunction
end
function myothersubfunc
% Another subfunction
end
Or, if you prefer specifying an input flag (to avoid any confusion associated with accidentally calling the function with no inputs as Jonas mentions in his comment), you could return the subfunction handles when the input argument data
is a particular character string. For example, you could change the input checking logic in the above code to this: