フィルターのクリア

HOW TO CHECK THE PARAMETERS RECEIVED IN THE FUNCTION CALL?

4 ビュー (過去 30 日間)
federica pasquali
federica pasquali 2018 年 8 月 2 日
コメント済み: OCDER 2018 年 8 月 9 日
dear all,
this is my function : function [] = print( value1 , value2, value3 ,value4,value5,value6,value7,value8)
value1 and value 2 are REQUIRED the others are OPTIONAL . I need to know how to check which parameters i've received and their value , because some of them could be numbers , characters . and i don't know how to do that. thanks

採用された回答

OCDER
OCDER 2018 年 8 月 2 日
I think input parser + varargin is what you need here.
Example:
%Changed the name "print" to "print2" to prevent overriding matlab built-in "print" function
function [] = print2(varargin)
P = inputParser;
addRequired(P, 'value1', @ischar);
addRequired(P, 'value2', @isnumeric);
addOptional(P, 'value3', [], @(x) ischar(x) || isnumeric(x));
parse(P, varargin{:});
P = P.Results; %Just so you can refer to value1 as P.value1, not P.Results.value1 (downside to inputParser)
%To get value1, use P.value1
Alternatively, you'll have to make your own input-parsing function
  10 件のコメント
federica pasquali
federica pasquali 2018 年 8 月 9 日
I really want to thank you for the help. you were very kind
OCDER
OCDER 2018 年 8 月 9 日
You're welcome!

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

その他の回答 (1 件)

federica pasquali
federica pasquali 2018 年 8 月 5 日
function [] = prova(ID , Folder , varargin)
P = inputParser;
addRequired( P , 'ID' );
addRequired( P , 'Folder' );
addOptional( P , 'Chain' , '' , @(x) ischar(x) );
addOptional( P , 'X' , [] , @(x) isnumeric(x) );
addOptional( P , 'Y' ,[] , @(x) isnumeric(x) );
addOptional( P , 'Z' ,[] , @(x) isnumeric(x) );
addOptional( P , 'Display' ,'', @(x) ischar(x) );
addOptional( P , 'Color' ,'' , @(x) ischar(x) );
parse(P , ID , Folder , varargin {:});
P = P.Results
end
if i'm calling prova('1a',folder,'b') it's going to work. but if i'm calling prova('1a',folder,30,40,60) without chain (before ='b') doesn't work , i received this error :The value of 'Chain' is invalid. It must satisfy the function: @(x)ischar(x). so i've tried to use try catch like this
try
addOptional( P , 'Chain' , '' , @(x) ischar(x) );
catch ME
end
but it doesn't work and i don't know how ho to built a ME object .

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by