MATLAB: Is there a method to better organize functions for experiments?

╄→гoц情女王★ 提交于 2019-12-03 14:50:41

Organization with a struct

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

For example a structure with 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);

and your function definition would be something like

function [model, threshold] = detect(setts)

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

Mixed approach

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

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

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

OOP approach

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.

For example

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!