Yam*_*eko 6 matlab arguments function function-parameter
我将进行一系列实验.评估的主要方法具有以下特征:
[Model threshold] = detect(...
TrainNeg, TrainPos, nf, nT, factors, ...
removeEachStage, applyEstEachStage, removeFeatures);
Run Code Online (Sandbox Code Playgroud)
在哪里removeEachStage,applyEstEachStage和,removeFeatures是布尔.你可以看到,如果我颠倒任何这些布尔参数的顺序,我可能会得到错误的结果.
MATLAB中是否有一种方法可以更好地组织,以最大限度地减少这种错误?或者,我可以使用任何工具来保护我免受这些错误的影响吗?
具有结构的组织
您可以输入struct具有这些参数的字段作为字段.
例如,具有字段的结构
setts.TrainNeg
.TrainPos
.nf
.nT
.factors
.removeEachStage
.applyEstEachStage
.removeFeatures
Run Code Online (Sandbox Code Playgroud)
这样,当您设置字段时,字段清晰,与函数调用不同,您必须记住参数的顺序.
然后你的函数调用变成了
[Model threshold] = detect(setts);
Run Code Online (Sandbox Code Playgroud)
你的函数定义就像是
function [model, threshold] = detect(setts)
Run Code Online (Sandbox Code Playgroud)
然后简单地替换例如paramwith 的出现setts.param.
混合的方法
如果您愿意,也可以将此方法与当前方法混合使用,例如
[Model threshold] = detect(in1, in2, setts);
Run Code Online (Sandbox Code Playgroud)
如果你想仍然显式包含in1和in2,并将其余部分捆绑在一起setts.
OOP方法
另一种选择是将检测变成一个类.这样做的好处是,detect对象将具有固定名称的成员变量,而不是结构,如果您在设置字段时输入错误,则只需创建带有拼写错误名称的新字段.
例如
classdef detect()
properties
TrainNeg = [];
TrainPos = [];
nf = [];
nT = [];
factors = [];
removeEachStage = [];
applyEstEachStage = [];
removeFeatures =[];
end
methods
function run(self)
% Put the old detect code in here, use e.g. self.TrainNeg to access member variables (aka properties)
end
end
Run Code Online (Sandbox Code Playgroud)