InputParser and function handle

Hi,
I want to use the InputParser Class to check the inputs of a function. Basically my function looks like this
function a = myfun(b,varargin)
p = inputParser;
addRequired(p,'b',@isnumeric);
addOptional(p,'fun',@mean,@(x) isa(x,'function_handle'));
parse(p,b,varargin);
a = p.Results.fun(p.Results.b);
If I call this function, say with
a = myfun(rand(10,1),'fun',@mean)
Matlab throws an error saying
Error using myfun (line 8)
Argument 'fun' failed validation @(x)isa(x,'function_handle').
What am I doing wrong and what is the best way to pass a function handle through the inputParser?
Regards, Anon

回答 (1 件)

Wolfgang
Wolfgang 2013 年 3 月 25 日

0 投票

You have to write:
parse(p,b,varargin{:});
In your case, the function handle is presented to the parser as part of a cell array (varargin) so it doesn't recognize it correctly.

1 件のコメント

Bjorn Gustavsson
Bjorn Gustavsson 2013 年 8 月 7 日
(I just ran into the same problem as Anon.) That varargin is a cell-array I understand, but shouldn't varargin{:} be the list of its contens, and that list is what the parse function should see?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeArgument Definitions についてさらに検索

タグ

質問済み:

2012 年 7 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by