1
votes

I have a certain function, say myFunc, that has an argument, say funcHandleArg, that should be a function_handle instance. However, this argument is not restricted to be one function_handle, but may in fact be a set of function_handles. Because Matlab does not accept non-scalar arrays of function handles [func_handle1, func_handle2], I must pass this argument as a cell array of function_handles {func_handle1, func_handle2}.

Now, my question is, how do I make sure that both funcHandleArg = func_handle1 and funcHandleArg = {func_handle1, func_handle2} are validated and accepted as input arguments. Summarising, I'd like something like this:

function output = myFunc(funcHandleArg, someOtherStuff)
arguments
    funcHandleArg function_handle "AND cells of function_handles"
    someOtherStuff otherStuff
end
output = someFunctionOf(funcHandleArg, someOtherStuff)
end 
1
You can use iscell and isa(funcHandleArg,'function_handle') to determine what type of variable you have. - David
I see your point, defining some validator function I could validate whether my argument is either a cell or a function_handle. However, this does not solve my problem, because: 1) I don't check whether my argument is a cell of function_handles 2) I don't have the easy-looking one-liner in the arguments block. - Sam

1 Answers

2
votes

You could write a simple validation function like this:

function myFunc(fcnOrFcns, otherArgs)
arguments
    fcnOrFcns {isOneOrMoreFunction}
    otherArgs
end
celldisp({fcnOrFcns, otherArgs});
end

function isOneOrMoreFunction(f)
isFcn = @(f) isa(f, 'function_handle');
assert(isFcn(f) || (iscell(f) && all(cellfun(isFcn, f))));
end