# Read and write multiple excel files.one after the other

61 views (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

dpb
on 24 Jul 2014

Edited: dpb
on 24 Jul 2014

Basically, what I'm saying is

SEfile = '<file1location>';

LBfile = '<file2location>';

dstfile = '<destinationfilelocation>';

d1=dir(fullfile(SEfile,'somesuitablewildcardpattern');

d2=dir(fullfile(LBfile,'somesuitablewildcardpattern');

for i=1:length(d1)

dat1=xlsread(d1(i).name); % read the first

dat2=xlsread(d2(i).name); % and the second...

% do whatever calculations on data desired here

dat=... % results of those calcs

xlswrite(fullfile(dstfile,'desiredoutputfilename',dat);

end

Salt the specifics of the sheet names and areas to suit and fixup output file naming convention as desired (with some modification of the input names one would presume).

##### 4 Comments

dpb
on 24 Jul 2014

### More Answers (8)

dpb
on 23 Jul 2014

KRUNAL
on 24 Jul 2014

Edited: KRUNAL
on 24 Jul 2014

##### 4 Comments

dpb
on 25 Jul 2014

dat1=dat1=xlsread(d1(i).name);

Hopefully that is

dat1=xlsread(d1(i).name);

instead.

dir returns a structure array whereas dat1 will be a double array, not a structure per the documentation for xlsread

You address it with the subscripts for the locations desired.

Read and work thru the tutorial information in the "Getting Started" section of the documentation at

http://www.mathworks.com/help/matlab/getting-started-with-matlab.html, particularly starting with the "Matrices and Arrays" and "Array Indexing" sections.

KRUNAL
on 28 Jul 2014

##### 1 Comment

dpb
on 28 Jul 2014

KRUNAL
on 28 Jul 2014

##### 1 Comment

dpb
on 28 Jul 2014

Tony Castillo
on 7 Nov 2019

Hello all,

I need your help so as to overcome and issue I am experiencing now, I need to write dowm in and excel spreadsheet a matriz of 100 columns by 18 rows. Previously, I only needed to make it 27 times, because of it I have been writing my 27 column by 18 rows matriz, doing the procedure shown below, but rigth now it is not efficient.

I do hope you can give me a key to enhance this.

sheet = 1;

Vector= [VL IL IbToda Pos Neg Vb pl PLavg pg EneIN EnCONV socmin socmax DeltaSOC Avg_SOC n MAPE RMSE FF]';

if i==1

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'b2:b20')

elseif i==2

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'c2:c20')

elseif i==3

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'d2:d20')

elseif i==4

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'e2:e20')

elseif i==5

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'f2:f20')

elseif i==6

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'g2:g20')

elseif i==7

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'h2:h20')

elseif i==8

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'i2:i20')

elseif i==9

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'j2:j20')

elseif i==10

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'k2:k20')

elseif i==11

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'l2:l20')

elseif i==12

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'m2:m20')

elseif i==13

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'n2:n20')

elseif i==14

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'o2:o20')

elseif i==15

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'p2:p20')

elseif i==16

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'q2:q20')

elseif i==17

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'r2:r20')

elseif i==18

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'s2:s20')

elseif i==19

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'t2:t20')

elseif i==20

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'u2:u20')

elseif i==21

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'v2:v20')

elseif i==22

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'w2:w20')

elseif i==23

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'x2:x20')

elseif i==24

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'y2:y20')

elseif i==25

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'z2:z20')

elseif i==26

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'aa2:aa20')

else

xlswrite('HOUSE_REAL.xlsx',Vector,sheet,'b2:ab20')

end

filename = 'HOUSE_REAL.xlsx';

A = {'VL(V)','IL(A)','IbToda(A)','Ib(+)','Ib(-)','Vb(V)',...

'Pico de consumo (W)','Potencia Media(W)','Pico de generación (W)',...

'Energía de entrada diaría (Wh)','Energía convertida diaría (Wh)',...

'SOC min (%)','SOC max (%)','DeltaSOC (%)','Avg_SOC (%)', ...

'Eficiencia (%)','MAPE (%)','RMSE','Fill Factor'}';

B={'REAL'};

xlRange = 'A2';

xlswrite(filename,A,sheet,xlRange)

xlswrite(filename,B,sheet,'A1:ab1')

##### 0 Comments

### See Also

### Community Treasure Hunt

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

Start Hunting!