how to write data from a loop to excel file
이전 댓글 표시
Io=0.86;
Iv=0.43;
for theta= 0:6:360
Imax = Io+Iv+2*cos (theta)*sqrt(Io.*Iv);
Imin = Io+Iv-2*cos (theta)*sqrt(Io.*Iv);
fringe_contrast =(Imax-Imin)/(Imax+Imin);
end
how do i store the values of theta, Imax, Imin and fringe_contrast in an excel file to show all the values generated from the loop?
답변 (1개)
Io=0.86;
Iv=0.43;
theta = 0:6:360; % Better use vectorization
Imax = Io+Iv+2*cos(theta) * sqrt(Io.*Iv);
Imin = Io+Iv-2*cos(theta) * sqrt(Io.*Iv);
fringe_contrast = (Imax-Imin)./(Imax+Imin); % Element-wise division
filename = 'data.xlsx';
A = {'theta','Imax','Imin','fringe_contrast'; theta.', Imax.', Imin.', fringe_contrast.'};
sheet = 1;
xlRange = 'B1';
xlswrite(filename, A, sheet, xlRange)
I don't have MATLAB installed for the laptop I am using right now, so I cannot check right now. Tell me if there is any problem.
댓글 수: 3
Added a couple of tweaks to coffee's original answer.
Io=0.86;
Iv=0.43;
theta = [0:6:360]; % Better use vectorization
Imax = Io+Iv+2*cos (theta)*sqrt(Io.*Iv);
Imin = Io+Iv-2*cos (theta)*sqrt(Io.*Iv);
fringe_contrast =(Imax-Imin)./(Imax+Imin); % Element-wise division
%
% A = {'theta','Imax','Imin','fringe_contrast'; theta.', Imax.', Imin.', fringe_contrast.'};
%
% Write the table headers. Kept it separately in case you want to use different headers.
tableHeaders = {'theta','Imax','Imin','fringe_contrast' };
%
% Export out using xlswrite.
filename = 'data.xlsx';
sheet = 1;
xlRange = 'A1';
xlswrite(filename, tableHeaders, sheet, xlRange)
%
% Main Array to report.
mainArrayToWrite = [theta.', Imax.', Imin.', fringe_contrast.'];
%
% Export Main Array to the same file.
filename = 'data.xlsx';
sheet = 1;
xlRange = 'A2';
xlswrite(filename, mainArrayToWrite, sheet, xlRange)
Kept the main array and the table headers separately in case you want to add different names, etc.
HTH.
alex kariuki
2015년 7월 8일
Hi Alex. Try Sid's nice corrected version.
Io=0.86;
Iv=0.43;
theta = 0:6:360; % Better use vectorization
Imax = Io+Iv+2*cos(theta) * sqrt(Io.*Iv);
Imin = Io+Iv-2*cos(theta) * sqrt(Io.*Iv);
fringe_contrast =(Imax-Imin)./(Imax+Imin);
tableHeaders = {'theta','Imax','Imin','fringe_contrast' };
filename = 'data.xlsx';
sheet = 1;
xlRange = 'A1';
xlswrite(filename, tableHeaders, sheet, xlRange)
mainArrayToWrite = [theta.', Imax.', Imin.', fringe_contrast.'];
filename = 'data.xlsx';
sheet = 1;
xlRange = 'A2';
xlswrite(filename, mainArrayToWrite, sheet, xlRange)
Using loop is often not ideal, because they take longer than vectorization. Checked it and it works. HTH
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!