2012-08-10 56 views
6

我將運行一組實驗。評價的主要方法有以下特徵:MATLAB:有沒有一種更好地組織實驗功能的方法?

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

其中removeEachStageapplyEstEachStage,並removeFeatures是布爾值。你可以看到,如果我顛倒任何這些布爾參數的順序,我可能會得到錯誤的結果。

在MATLAB中有沒有一種方法允許更好的組織來減少這種錯誤?或者有什麼工具可以用來保護我免受這些錯誤的侵害?

+2

你可能會發現一些問題的答案從這裏有用:如何處理在MATLAB函數的參數名稱/值對(http://stackoverflow.com/q/2775263/97160) – Amro 2012-08-10 18:40:30

+1

檢出文件交換中的'parseArgs'。我每天都用它:http://www.mathworks.com/matlabcentral/fileexchange/10670-parseargs-simplifies-input-processing-for-functions-with-multiple-options – slayton 2012-10-02 19:45:03

+0

@slayton - 我不知道這一個。我會測試它。謝謝! :D – Yamaneko 2012-10-03 13:58:04

回答

6

組織與結構

,你可以輸入一個struct具有這些參數,因爲它的領域。

例如帶有字段

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

當您設置域顯然領域是什麼,不像一個函數調用,你必須記住參數的順序這樣的結構。

那麼你的函數調用變得

[Model threshold] = detect(setts); 

和你的函數定義是類似

function [model, threshold] = detect(setts) 

然後只需更換的例如在發生paramsetts.param

混合的方法

您可以與您當前的一個,如果你願意,也如混合這種方法

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

,如果你想仍然明確包括in1in2,並捆綁到其他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 
+1

感謝您的回答!這很清楚。我發現通過OOP解決方案非常有趣。另外,我認爲[開銷數量](http:// stackoverflow。com/a/1745686/957997)加入並沒有太大的區別。 – Yamaneko 2012-08-10 21:29:25

相關問題