MATLAB: Operation on structure fields with common part of the field name

charfieldsMATLABstringsstructures

Hi, I have succesfully imported several mat files into a structure with fields names beginning either by "DUT" or "REF". Now I need to divide all the fileds beginning by "DUT" to the corresponding one beginning by "REF", for example, I want to compute DUT_2/REF_2, DUT_4/REF_4 etc…

Best Answer

  • S.DUT_2 = 2;
    S.REF_2 = 3;
    S.DUT_4 = 4;
    S.REF_4 = 5;
    C = fieldnames(S);
    U = unique(regexp(C,'\d+$','once','match'))
    F = @(n)S.(['DUT_',n]) ./ S.(['REF_',n]);
    Z = cellfun(F,U)
    Giving:
    U =
    '2'
    '4'
    Z =
    0.66667
    0.8