How to write data in an excel sheet with one separate column for each iteration in a for loop?

4 views (last 30 days)
I have a matlab script that has a for loop for 50 iterations. For each iteration the output is a 4097x1 matrix. I want to write each iteration in a separate sequential column so that at the end I have a 4097x50 matrix in the same excel sheet.
Please note that in the script the output is also getting as one row for all the iterations (that means 204850 rows (4097x50=204850)). If I can get all the results in a matrix form 4097x50 in matlab that also will do the purpose.
Can anyone please help me on this??
Thank You.........!!!!

Accepted Answer

Image Analyst
Image Analyst on 24 Nov 2014
output = zeros(4097, 50); % Preallocate
for iteration = 1 : 50
output(:, iteration) = GetColumnVector(); % Stuff your 4097 elements into one column.
end
xlswrite(filename, output, 'A1');
  2 Comments
Tharindu
Tharindu on 24 Nov 2014
Edited: Image Analyst on 24 Nov 2014
Many thanks for your prompt response. I am little bit confused how to in-cooperate it into my script. Can you please help me how should I include this into following script. I am sorry I am relatively new to Matlab coding;
a=xlsread('V02_All_column5.xlsx');
b=xlsread('V02_All_column6.xlsx');
H1 = zeros( 4097, 1, 50 );
for i = 1:50
z=a(:,i);
x=b(:,i);
Fs=3200;
L=length(z);
NFFT=2^nextpow2(L);
window=hann(L/2);
noverlap=8;
[Pff(:,:),f]=pwelch(z(:,:),window,noverlap,NFFT,Fs);
[Pxx(:,:),f]=pwelch(x(:,:),window,noverlap,NFFT,Fs);
[Pfx(:,:),f]=cpsd(z(:,:),x(:,:),window,noverlap,NFFT,Fs);
[Pxf(:,:),f]=cpsd(x(:,:),z(:,:),window,noverlap,NFFT,Fs);
H1(:,:,i)=Pfx(:,:)./Pff(:,:);
H2(:,:)=Pxx(:,:)./Pxf(:,:);
end
I want to get the output real(H1) in a separate column for each iteration.
Many thanks again.

Sign in to comment.

More Answers (1)

Tharindu
Tharindu on 24 Nov 2014
I got the desired output by adjusting like this; output = zeros(4097, 50); % Preallocate
for i = 1 : 50 output(:, i) =real(H1(:,1,i)); % Stuff your 4097 elements into one column. end xlswrite('1.xlsx', output, 'A')
Many thanks for your comments. It really helped.

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!