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

I will run a series of experiments. The main evaluated method has the following signature:

[Model threshold] = detect(... TrainNeg, TrainPos, nf, nT, factors, ... removeEachStage, applyEstEachStage, removeFeatures); 

where removeEachStage , applyEstEachStage and removeFeatures are boolean. You can see that if I reverse the order of any of these boolean parameters, I may get incorrect results.

Is there a way in MATLAB that can improve the organization to minimize such an error? Or is there any tool I can use to protect against these errors?

+6
source share
1 answer

Organization with structure

You can enter a struct that has these parameters as fields.

For example, a structure with fields

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

Thus, when you set fields, it becomes clear what a field is, in contrast to a function call, where you need to remember the order of parameters.

Then your function call becomes

 [Model threshold] = detect(setts); 

and your function definition will be something like

 function [model, threshold] = detect(setts) 

Then just replace the entries, for example. param with setts.param .

Mixed approach

You can also mix this approach with your current if you want, for example.

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

if you want to explicitly specify in1 and in2 , and add the rest to setts .

OOP approach

Another option is to include discovery in the class. The advantage of this is that the detect object will have member variables with fixed names, unlike structures, where if you create a typo when setting up the field, you simply create a new field with the wrong name.

for instance

 classdef detect() properties TrainNeg = []; TrainPos = []; nf = []; nT = []; factors = []; removeEachStage = []; applyEstEachStage = []; removeFeatures =[]; end methods function run(self) % Put the old detect code in here, use eg self.TrainNeg to access member variables (aka properties) end end 
+6
source

Source: https://habr.com/ru/post/922602/


All Articles