Search code examples
matlabstructurefill

Fill structure more efficient


How can a structure i.e. 'settings' be filled more easily than with this code:

settings(1).exposure = 1;      
settings(1).rebalancing = 0;   
settings(2).exposure = 0;      
settings(2).rebalancing = 0;   
settings(3).exposure = 1;
settings(3).rebalancing = 1;
settings(4).exposure = 0;
settings(4).rebalancing = 1;
settings(5).exposure = 'benchmark';
settings(5).rebalancing = 0;
settings(6).exposure = 'benchmark';
settings(6).rebalancing = 1;

Solution

  • You can compress it using the struct function:

    >> s = struct('exposure',{1,0,1,0,'benchmark','benchmark'},'rebalancing',{0,0,1,1,0,1});
    >> s(6)
    ans = 
           exposure: 'benchmark'
        rebalancing: 1
    

    The array literals can be replaced by any variable that contains your data, as long as all arrays are conforming in size.