MATLAB: How to store Values in a Loop

how to save the loop data to an excel file in a multilooped program ?stephen trainor 0 flag running through a loop and storing values

for t=0:1:10
L(t) = 3*cos(t);
M(t) = diag([L 5*cos(t) 7*cos(t)]);
K(t) = [12e5*cos(t) 18e5*cos(t) 34e5*cos(t);18e5*cos(t) 15e5*cos(t) 12e5*cos(t);13e5*cos(t) 12e5*cos(t) 22e5*cos(t)];
[X,e] = polyeig(K, M)
o = sqrt(e);
fprintf('the freqeuncy is"%d" \n',o)
writematrix(o,'hello/tt.xlsx','sheet',1,'range','A1:A30');
end
Hi, From the above code I am trying to store 10 time series in a single excel file but I can't able to do it and getting a error like "Array indices must be positive integers or logical values". Can anyone please help me in this.
Thank you in Advance.

Best Answer

for t=0:10
L = 3*cos(t);
M = diag([L 5*cos(t) 7*cos(t)]);
K = [12e5*cos(t) 18e5*cos(t) 34e5*cos(t);18e5*cos(t) 15e5*cos(t) 12e5*cos(t);13e5*cos(t) 12e5*cos(t) 22e5*cos(t)];
[X,e] = polyeig(K, M)%X is a 3x3 matrix, e is 3x1 vector
o(:,t+1) = sqrt(e);%o is a 3x11 matrix when the loop finishes
end
writematrix(o,'hello/tt.xlsx','sheet',1,'range','A1:A30');
The only thing you are writing to file is the variable o; therefore it is the only thing needing to be in an array.