Asked by naresh bhimchand
on 11 Nov 2019 at 9:46

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.

Answer by David Hill
on 12 Nov 2019 at 1:21

Accepted 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.

naresh bhimchand
on 12 Nov 2019 at 1:43

Walter Roberson
on 12 Nov 2019 at 2:34

If your o is ending up as 3x11 then it has all of the iterations in it.

Note that you are calculating 33 values not 30, because 0:10 is 11 iterations not 10; and that you are asking to write the 3x11 martrix into a 30x1 array. Perhaps you should change the 0:10 to 0:9 and perhaps you should writematrix o(:) instead of o

naresh bhimchand
on 12 Nov 2019 at 2:39

I got it, bro.Thanks :)

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Walter Roberson (view profile)

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/490392-how-to-store-values-in-a-loop#comment_765927

## naresh bhimchand (view profile)

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/490392-how-to-store-values-in-a-loop#comment_766230

## Walter Roberson (view profile)

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/490392-how-to-store-values-in-a-loop#comment_766235

## naresh bhimchand (view profile)

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/490392-how-to-store-values-in-a-loop#comment_766238

Sign in to comment.