How to write an ''if''condition to determine what variable are in a function ?

example sometime I will use : f=@(x,t) 8x+3t
or i will use
f=@(x) cos(x)
now I want an if function thats looks like this :
if f depends only on x
do gjgjgjgjg
elseif f depends on x and t
do jnfundinb
else
do kgnmpofgn

 Accepted Answer

fstring=func2str(f);
if contains(fstring,'@(x)') %depends only on x
...
elseif contains(fstring,'@(x,t)') %depends on x and t
...
else
...
end

More Answers (1)

If the only thing that actually matters is the number of input arguments (and not their specific names), then you can trivially use nargin like this:
fun = @(...)...;
switch nargin(fun)
case 0
...
case 1
...
...
end
This will be more efficient than converting the entire function to string.

Asked:

on 13 Apr 2018

Edited:

on 13 Apr 2018

Community Treasure Hunt

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

Start Hunting!