且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MATLAB:有没有一种方法可以更好地组织实验函数?

更新时间:2022-12-17 10:48:29

组织结构

您可以输入一个 struct ,它具有这些参数作为它的字段.

You could input a struct that has these parameters as it's fields.

例如带有字段的结构

setts.TrainNeg
     .TrainPos
     .nf
     .nT
     .factors
     .removeEachStage
     .applyEstEachStage
     .removeFeatures

这样,当您设置字段时,字段是什么就一目了然,这与函数调用不同,您必须记住参数的顺序.

That way when you set the fields it is clear what the field is, unlike a function call where you have to remember the order of the parameters.

那么你的函数调用就变成了

Then your function call becomes

[Model threshold] = detect(setts);

你的函数定义类似于

function [model, threshold] = detect(setts)

然后简单地替换例如的出现paramsetts.param.

Then simply replace the occurrences of e.g. param with setts.param.

混合方法

如果您愿意,也可以将此方法与当前方法混合使用,例如

You can also mix this approach with your current one if you prefer, e.g.

[Model threshold] = detect(in1, in2, setts);

如果您仍想明确包含 in1in2,并将其余部分捆绑到 sets 中.

if you wanted to still explicitly include in1 and in2, and bundle the rest into setts.

面向对象的方法

另一种选择是将检测变成一个类.这样做的好处是 detect 对象将具有固定名称的成员变量,而不是结构体,如果在设置字段时输入错误,则只需创建一个名称拼写错误的新字段.

Another option is to turn detect into a class. The benefit to this is that a detect object would then have member variables with fixed names, as opposed to structs where if you make a typo when setting a field you just create a new field with the misspelled name.

例如

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