-1

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;
InDubio
  • 67
  • 1
  • 9

2 Answers2

2

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.

TroyHaskin
  • 8,361
  • 3
  • 22
  • 22
0

you can create an array / matrix with [ 1 2 3 4 5 6]

then in a for loop, for each number ask an input

i=1:6;
for i:6;
    settings(i).exposure=input(...);
    settings(i).rebalancing=input(...);
end

I think you should be able with this. (its been sometime since I last used a computer with MatLab so I can't confirm)

  • First, [you shouldn't use `i` as a variable name](http://stackoverflow.com/q/14790740/4221706), as it denotes the imaginary unit. Second, don't define `i` before the for loop, as it will just be redefined on the next line. Third, the syntax is `for i=1:6`. – hbaderts Nov 12 '16 at 19:18
  • yeah, sorry. I've been using more JAVA than Matlab lately and got caught in the habit. –  Nov 13 '16 at 01:22