Function handle in varargin not recognized
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
When i am trying to input a function manually the scripts doesent recognize x ( Unrecognized function or variable 'x'. ) see pictures
How am i using the function handle wrong?
Thanks
function drawfunction(varargin)
switch nargin
case 0
disp('0 inparam. chosen')
f = @(x) exp(atan(x)+sin(x))+log(2+x.^4)-(cos(3*x)/(1+x.^2));
x = linspace(-3,8);
color = 'r:';
case 1
disp('1 inparam. chosen')
f = @(x) varargin(1);
x = linspace(-3,8);
color = 'r:';
case 2
disp('2 inparam. chosen')
f = @(x) varargin(1);
x = varargin(2);
color = 'r:';
case 3
disp('3 inparam. chosen')
f = @(x)varargin(1);
x = varargin(2);
color = varargin(3);
otherwise
error('Unexpexted inputs')
end
plot(f(x),color)
No input:

One input: (same function)

0 Kommentare
Antworten (1)
Voss
am 16 Jun. 2022
You're calling drawfunction with an expression that MATLAB tries to evaluate before sending the result of that evaluation in to drawfunction.
What you mean to do is to send a function handle to drawfunction, so call it like this:
drawfunction( @(x)exp(atan(x)+sin(x)) ) % ( etc., long expression )
Then, inside drawfunction, varargin{1} (note the curly braces, by the way) is that function handle you passed in, so there's no need to do this:
f = @(x)varargin(1) % neither one of these is correct
f = @(x)varargin{1}
It's just this:
f = varargin{1}; % varargin{1}, i.e., the first input argument, is already a function handle
3 Kommentare
Voss
am 16 Jun. 2022
Bearbeitet: Voss
am 16 Jun. 2022
As you've discovered, you cannot send missing arguments like that
drawfunction( , , 'b:')
If you want to use an input argument, then all previous input arguments must also be specified.
However, you can write your function such that it handles certain values specially. For instance, you might use the empty array [] as a special value that indicates to the function that the default value for that argument should be used (this is only useful if [] is not a valid value for that argument).
If you do that, then you could call drawfunction like this:
drawfunction([], [], 'b:')
to get the default function and x values. Of course, you also have to set up drawfunction to handle [] appropriately, which is easy if you use the argument handling structure I showed in my last comment:
function drawfunction(varargin)
if nargin >= 1 && ~isempty(varargin{1}) % at least one argument given and it's not empty
if ischar(varargin{1})
f = str2func(['@(x)' varargin{1}]);
else
f = varargin{1};
end
else
f = @(x)exp(atan(x)+sin(x)); % default function
end
if nargin >= 2 && ~isempty(varargin{2}) % at least two arguments given and argument 2 is not empty
x = varargin{2};
else
x = linspace(-3,8); % default x
end
if nargin >= 3 && ~isempty(varargin{3}) % at least three arguments given and argument 3 is not empty
color = varargin{3};
else
color = 'r:'; % default color/line-spec
end
if nargin >= 4
% error('Unexpexted inputs');
warning('drawfunction: Extra arguments ignored.');
else
disp(sprintf('%d inparam. chosen',nargin));
end
plot(x,f(x),color);
end
(Note that I changed
plot(f(x),color);
to
plot(x,f(x),color);
because I think that makes more sense.)
Siehe auch
Kategorien
Mehr zu Argument Definitions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

