using a "for loop" for writing a code more compactly

조회 수: 6 (최근 30일)
Sabbas
Sabbas 2012년 7월 4일
Dear all,
My analysis is repeated every block of 3 sheets of an excel file as follows
clear all
xlfilename = 'xa.xls' %it contains many sheets
finaldata = [];
%Sheets 1 2 3
ii = 1
[num3,txt3,raw3] = xlsread(xlfilename,ii+2); % Reads in sheet ii
[num2,txt2,raw2] = xlsread(xlfilename,ii+1); % Reads in sheet ii
[num1,txt1,raw1] = xlsread(xlfilename,ii); % Reads in sheet ii
size(raw3)
size(raw2(:,3:end))
size(raw1(:,3:end))
raw2(1,:)=[];
raw1(1,:)=[];
finaldata0 = [finaldata raw3 raw2(:,3:end) raw1(:,3:end) ];
outfile0 = 'processedexcel0.xls'
xlswrite(outfile0,finaldata);
The same analysis is repeated for the next 3 sheets
%Sheets 4 5 6
ii = 4
[num6,txt6,raw6] = xlsread(xlfilename,ii+2); % Reads in sheet ii
[num5,txt5,raw5] = xlsread(xlfilename,ii+1); % Reads in sheet ii
[num4,txt4,raw4] = xlsread(xlfilename,ii); % Reads in sheet ii
size(raw6)
size(raw5(:,3:end))
size(raw4(:,3:end))
raw5(1,:)=[];
raw4(1,:)=[];
finaldata1 = [finaldata raw6 raw5(:,3:end) raw4(:,3:end) ];
outfile1 = 'processedexcel1.xls'
xlswrite(outfile1,finaldata1);
Because this process is repeated 40 times, is there a more compact/quick/advanced way of writing this code?
thanks in advance
  댓글 수: 2
Jan
Jan 2012년 7월 4일
Du you think that clear all is useful? Besides clearing the variables, it removes all loaded functions from the memory and reloading them wastes a lot of time.
Sabbas
Sabbas 2012년 7월 4일
thanks Simon. I use it only at the beginning

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2012년 7월 4일
편집: Image Analyst 2012년 7월 4일
You need to use ActiveX so that you only launch and shutdown Excel once to read the sheets instead of 40*3=120 times and another 40 launches and shutdowns to call xlswrite. You should be able to find plenty of ActiveX examples in Answers and on MATLAB Central. I know I've posted some before.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by