用于函数顺序求值的本机MATLAB函数?
因此,我试图在MATLAB函数中完善我的输入验证,在这个过程中,我最终创建了以下函数,这确实使我的代码更简洁。我把它全部放在这里,以防有人发现它有用:用于函数顺序求值的本机MATLAB函数?,matlab,function,validation,call,sequential,Matlab,Function,Validation,Call,Sequential,因此,我试图在MATLAB函数中完善我的输入验证,在这个过程中,我最终创建了以下函数,这确实使我的代码更简洁。我把它全部放在这里,以防有人发现它有用: function [] = callFunctions(varargin) %callFunctions Calls a sequence of functions on their respective arguments. % The inputs are expected as funcHdl1, {input1}, funcHdl2,
function [] = callFunctions(varargin)
%callFunctions Calls a sequence of functions on their respective arguments.
% The inputs are expected as funcHdl1, {input1}, funcHdl2, {input2}, ...
% Then callFunctions calls sequentially func1(input1); func2(input2); ...
%% === INPUT VALIDATION ===
% Check that the callFunctions has at least one argument
message = 'The number of arguments must be positive.';
assert(nargin > 0, message);
% Check that the callFunctions has an even number of arguments
message = 'The number of arguments must be even.';
assert(mod(nargin,2)==0,message);
% Name the handles
numHandles = nargin/2;
handleNames = cell(1,numHandles);
inputNames = cell(1,numHandles);
for k=1:numHandles
handleNames{k} = 'handle';
handleNames{k} = cat(2, handleNames{k}, int2str(k));
inputNames{k} = 'input';
inputNames{k} = cat(2, inputNames{k}, int2str(k));
end
% Function to check that the inputs are function handles
isValidFunctionHdl = @(x) validateattributes(x, {'function_handle'}, {});
% Create an input parser
p = inputParser;
% Add all arguments to check if they are handles. Don't check the inputs.
for k=1:numHandles
p.addOptional(handleNames{k}, 0, isValidFunctionHdl);
p.addOptional(inputNames{k}, 0);
end
% Parse the input
p.parse(varargin{:});
%% === PROGRAM ===
% Evaluate all the functions on their inputs
for k=1:numHandles
feval(varargin{2*k-1},varargin{2*k}{:});
end
end
现在,最后一个名为%%==程序===
的部分是重要的部分。它只对函数func1、func2、func3、…
的各自输入input1、input2、input3、…
求值,所有这些都作为callFunctions的输入。它被称为:
callFunctions(funcHdl1, {input1}, funcHdl2, {input2}, ...)
我的问题很简单:是否有一个本机的MATLAB函数可以做callFunctions
所做的事情?我在输入验证期间使用它(与assert
和validateattributes
结合使用)。因此,我打算在我编写的几乎每个函数中使用callFunctions
。我想使用尽可能少的自定义函数
本
编辑:我不想为了保持文章简短而进入这个话题。但是是的,在某种情况下,这对我来说是必要的。这就是我使用输入解析器进行输入验证的时候。我经常需要检查多个条件,如以下(完全虚构的示例):
%组成了一个用于说明上下文的函数
函数[]=myFunction(输入)
%创建一个输入解析器
p=输入解析器;
%创建函数以验证输入。必须是至少有一个假值的2d逻辑数组
isValidArray=@(x)validateAttribute(x,{'logical'},{'2d'});
hasatleastoneflse=@(x)assert(length(find(x))
这使我的代码更加清晰,因为它允许我在调用addRequired
时组合多个validateattributes
和assert
语句。据我所知,目前还没有类似的语句存在。。。我不知道为什么会有
基本上你是在交易:
func1(input1);
func2(input2);
为此:
callFunctions(@func1, {input1}, @func2, {input2})
我所看到的只是一系列输入检查callFunctions
是否正确使用。假设每个函数都写得很好,并且有自己的断言等。。。我根本不需要这个。除了让它更难阅读之外
您可以完全跳过它,也可以为FCN句柄和输入列表创建一个简单的for循环。每个函数都会处理自己的错误,并且您不会在一些看起来非常简单的事情上添加一堆代码
编辑:
对于您的示例用法:为什么不这样做?对我来说,这比你的例子要“干净”得多,这似乎是你的目标
function [] = myFunction(input)
% Create an input parser
p = inputParser;
% Must be a 2d logical array with at least one false value
addRequired(p, 'input', @(x) validateattributes(x, {'logical'}, {'2d'}));
p.parse(input);
assert(length(find(input)) < length(input), 'Must be at least one false.')
disp('Running rest of code with good inputs!')
有没有一个本地的MATLAB函数可以实现这一点?确切地说,这就是什么?这就是callFunctions
所做的。对字符串单元格进行eval
不是更简单的方法吗?@Ben然后你应该解释函数的作用。这比要求潜在的回答者阅读你的代码并推断它的意图要容易得多。我的评论太长了,所以我只是把它作为一个答案添加了进去。基本上没有Matlab没有这个内置功能&我看不出它有什么好处。我在我的帖子中添加了一个编辑来显示我使用它的上下文。我一直这样使用它。我不想深入细节,因为我想把重点放在函数本身上。我将输入验证放在callFunctions
的代码中,以防有人只是想复制和粘贴callFunctions
。正如我在我的帖子中所说,它清楚地表示为输入验证,您可以忽略它。我说重要的部分是最后一部分。谢谢你的回答!如果没有本机函数,我将只使用我的函数。简而言之,我被迫这样做,因为addRequired
方法只接受单个验证函数作为输入。对不起,把大家都弄糊涂了。也许我会重新写一篇文章,把问题解释得更清楚。@Ben请看我对您的示例用法的编辑。在我看来,没有callFunctions
的代码看起来更干净,并且可以处理同样糟糕的输入情况。。。除非我遗漏了什么。谢谢,这确实也很有效。我只是觉得在inputParser
中处理所有错误的形式更好、更连贯。通过这种方式,我定义了什么使我的输入只有效一次,然后我只验证它一次。如果你叫我强迫症,我能理解。但也许你的解决方案最终确实是最简单的。
function [] = myFunction(input)
% Create an input parser
p = inputParser;
% Must be a 2d logical array with at least one false value
addRequired(p, 'input', @(x) validateattributes(x, {'logical'}, {'2d'}));
p.parse(input);
assert(length(find(input)) < length(input), 'Must be at least one false.')
disp('Running rest of code with good inputs!')
>> myFunction(ones(2,2,2))
Error using myFunction (line 6)
The value of 'input' is invalid. Expected input to be
one of these types:
logical
Instead its type was double.
>> myFunction(true(2,2,2))
Error using myFunction (line 6)
The value of 'input' is invalid. Expected input to be two-dimensional.
>> myFunction(true(1,2))
Error using myFunction (line 7)
Must be at least one false.
>> myFunction([true false])
Running rest of code with good inputs!