如何推广Matlab函数?

时间:2017-12-21 10:49:59

标签: matlab function

我有一个Matlab功能。我需要概括这个功能。此代码的目的是检查TableTemp中的这个IndicMP,如果它存在,那么我们提取相关的年龄限制,例如:Age_Limite_DC,Age_Limite_IT,Age_Limite_Ch和Transfert_Prime_IT_DC。我的想法是概括,传递参数以找出" Type_pret" (可能是我错了)因为我是Matlab的初学者,有人可以帮我编码一个更通用的函数,可以在更一般的上下文中使用吗?

function Structure = optimisation_function()

Data = load('Data.mat');

Structure = Data.Structure;
TableTemp = Data.TableTemp;


Age_Limite_DC          = zeros(size(Structure,1),1);
Age_Limite_IT          = zeros(size(Structure,1),1);
Age_Limite_CH          = zeros(size(Structure,1),1);
Transfert_Prime_IT_DC  = zeros(size(Structure,1),1);

for IndexMPAL = 1 : length(Structure.AnneeSouscription)

    % Determine Type_Pret (Loan Type)
    if ~isempty(strfind(Structure.Type_Pret{IndexMPAL},'A'))
        Type_Pret = 'A';
    elseif ~isempty(strfind(Structure.Type_Pret{IndexMPAL},'B'))
        Type_Pret = 'B';
    elseif ~isempty(strfind(Structure.Type_Pret{IndexMPAL},'C'))
        Type_Pret = 'C';
    elseif ~isempty(strfind(Structure.Type_Pret{IndexMPAL},'D'))
        Type_Pret = 'D';
    elseif ~isempty(strfind(Structure.Type_Pret{IndexMPAL},'E'))
        Type_Pret = 'E';
    end
    MP_CP       = Structure.NomCodeProduit(IndexMPAL);
    MP_AnSous   = Structure.AnneeSouscription(IndexMPAL);
    MP_TypePret = Type_Pret;
    IndicCP     = strcmp(MP_CP, TableTemp.CodeProduit);
    IndicAS     = MP_AnSous== TableTemp.AnneeSouscription;
    IndicTP     = strcmp(MP_TypePret, TableTemp.TypePret);
    IndicMP     = IndicCP & IndicAS & IndicTP;        

    if ~any(IndicMP)
        Msg = strcat('CodeProduct:',MP_CP{1}, ', Année Souscription:', num2str(MP_AnSous), ', Type Prêt:', MP_TypePret);
        error('Error', Msg)
    else
        Age_Limite_DC(IndexMPAL,1) = TableTemp.Age_Limite_DC(IndicMP,1);
        Age_Limite_IT(IndexMPAL,1) = TableTemp.Age_Limite_IT(IndicMP,1);
        Age_Limite_CH(IndexMPAL,1) = TableTemp.Age_Limite_CH(IndicMP,1);
        Transfert_Prime_IT_DC(IndexMPAL,1)= 
        TableTemp.Transfert_Prime_IT_DC(IndicMP,1);
    end
end

Structure.Age_Limite_DC          = Age_Limite_DC;
Structure.Age_Limite_IT          = Age_Limite_IT;
Structure.Age_Limite_CH          = Age_Limite_CH;
Structure.Transfert_Prime_IT_DC  = Transfert_Prime_IT_DC;

end

1 个答案:

答案 0 :(得分:1)

使用单元格数组可以简化if/elseif

liststr = {'A','BB','C','D','E'}; % builds a cell array, each cell contains a string
Positive_matches = strfind(liststr,Structure.Type_Pret{IndexMPAL}) % returns a list for each cell of the indices where the element was found (empty if none)
Index = find(~cellfun('isempty', Positive_matches )) % converts the previous list into a logical 0/1 array indicating whether an item was found (1) or not (0)
% if isempty(Index); continue; end % If no index is found, to avoid an error in the next instruction, skips the rest of the code. 
Type_Pret = liststr(Index(1));

如果您的列表和Type_Pret strcmp`中的Structure? , you can use相同:

liststr = {'A','B','C','D','E'};
if any(strcmp(Structure.Type_Pret,liststr))
    Type_Pret = Structure.Type_Pret
else
    % handle error
end

您也可以直接在Structure.Age_Limite_DC上工作,而无需使用Age_Limite_DC