It's a little hard to describe exactly what I'm trying to do, but the code below should make it clear. Basically I'm trying to populate an array with elements from another array, with the elements specified dynamically. The closest I've been able to come is the code below. I know it's best to avoid Eval, so if someone has a better, more elegant solution I'd love to hear it. I'm very new to Matlab, so I'm probably missing something easy. Thanks, J.C.
totalValue = rand(1, 10);startSegmentEval = [1, 2, 3, 4, 5];endSegmentEval = [3, 4, 5, 6, 7];A = 'sum(totalValue(';B = ':';C = ')';D = int2str(startSegmentEval');E = int2str(endSegmentEval');F = ';';temp = [repmat(A, numel(D), 1), D, repmat(B, numel(D), 1), E, ... repmat(C, numel(D), 1), repmat(C, numel(D), 1)];a(1:5) = eval(temp);
Best Answer