我想出了以下解決方案:
function vout = test(timeSeries, hurdleRate, varargin)
p = inputParser;
expectedTypes = {'none', 'cash' 'absHurdleRate'};
default_numCashTicker = [];
default_absHurdleRate = [];
p.addRequired('timeSeries');
p.addRequired('hurdleRate',@(x) any(validatestring(x,expectedTypes)));
p.addParameter('numCashTicker', default_numCashTicker);
p.addParameter('absHurdleRate', default_absHurdleRate);
parse(p, timeSeries, hurdleRate, varargin{:});
numCashTicker = p.Results.numCashTicker;
switch hurdleRate
case 'none'
if ~isequal(numCashTicker, default_numCashTicker)
throw(MException('test:ParsingError', ...
'"none" does not require the "numCashTicker" parameter values"'));
elseif ~isequal(absHurdleRate, default_absHurdleRate)
throw(MException('test:ParsingError', ...
'"none" does not require the "absHurdleRate parameter values"'));
end
case 'cash'
if ~isnumeric(numCashTicker)
throw(MException('test:ParsingError', ...
'"cash" requires a numeric "numCashTicker" parameter value'));
elseif ~isequal(absHurdleRate, default_absHurdleRate)
throw(MException('CSmomRoC:ParsingError', ...
'"cash" does not require the "absHurdleRate parameter values"'));
end
case 'absHurdleRate'
if ~isnumeric(absHurdleRate)
throw(MException('CSmomRoC:ParsingError', ...
'"absHurdleRate" requires a numeric "absHurdleRate" parameter value'));
elseif ~isequal(numCashTicker, default_numCashTicker)
throw(MException('CSmomRoC:ParsingError', ...
'"absHurdleRate" does not require the "numCashTicker" parameter value.'));
end
end
...
...
...
end;
能否請您發佈的代碼示例?也不要要求「最佳策略」,因爲它太籠統 - 使它更具體。 – Rotem