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

1 回表示 (過去 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 Center および File ExchangeSpreadsheets についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by