noufal
noufal

Reputation: 970

Writing data array from a loop into consecutive columns of an excel file

This is a part of my code.

a = 0;
for i = -3:3
    for j = 1:10
        a(j) = j^i;
    end
    xlswrite('out.xls', a','A1')
end

Here I am getting only the last value of a in the out.xlx file. But I want to fill the values of a for each iteration of i in consecutive columns of an Excel file . Any suggestions ? Update I want output like this: -

1.0000   1.0000   1.0000   1  .....
0.1250   0.2500   0.5000   1  .....
0.0370   0.1111   0.3333   1  .....
0.0156   0.0625   0.2500   1  .....
0.0080   0.0400   0.2000   1  .....
0.0046   0.0278   0.1667   1  .....
0.0029   0.0204   0.1429   1  .....
0.0020   0.0156   0.1250   1  .....
0.0014   0.0123   0.1111   1  .....
0.0010   0.0100   0.1000   1  .....

and so on....

Upvotes: 0

Views: 4880

Answers (2)

Dedek Mraz
Dedek Mraz

Reputation: 814

You have produced a 1D vector instead of a 2D matrix. This should work:

a = 0;
for i = -3:3
    for j = 1:10
        a(j,i+4) = j^i;
    end
    xlswrite('out.xls', a',['A' num2str(i+4) ':J' num2str(i+4)])
end

An even better solution would be to vectorize everything:

[X,Y] = meshgrid(-3:3,1:10)
a = X.^Y
xlswrite('out.xls', a','A1:J7')

@EitanT, @HighPerformanceMark: Changed the answer to reflect your comment. I guess today is not my day - edited 4 times :)

Upvotes: 1

Kiran
Kiran

Reputation: 8528

You are entering the value a in the cell A1. So it is taking only the last value of the array a. You need to give the entire range in order to input the complete array.

Try something like this :

 xlswrite('out.xls', a','A1:J1') 

I hope it helps.

Upvotes: 0

Related Questions