Read and write multiple excel files.one after the other
Show older comments
I have 2 folders each containing 4 excel files. Then I need to do arithmetic calculation between 1st excel file of folder 1 with 1st excel file stored in folder 2 and write result in 1st blank excel file that is stored in folder 3(Arithmetic operation I am able to do). This process I want to do for all the files in the two folders. Is it possible? If so what code do I need to add to my existing code. Should I use 'For' loop?
Accepted Answer
More Answers (8)
dpb
on 23 Jul 2014
1 vote
I am extremely partial to the dir solution; in your case return two directory structures; one for each of the two directories and iterate over them simultaneously in a loop. You'll of course have to ensure you've go the consistent number of files in the two subdirectories and that you've got the proper ones at the same time but those are details...
4 Comments
KRUNAL
on 24 Jul 2014
dpb
on 24 Jul 2014
Don't know about that error, if I take
sheet = 'sheet1';
a = {'BHS_S'};
xlswrite('work.xls',a,sheet,'AA1');
it works fine here. Note I removed much of the extra obfuscation and complexity you've added--there's no need for the extra [] nor is there any real point in cell(1,1), you're not filling a large array so the direct assignment will be ok. Also, there's no need for the explicit reference when just assigning a single cell-string. Also note there's no need for the copy.
Your subdirectories, etc., obviously don't exist here so I just used a hardcoded file name; you'll have to debug that part.
Try the above from the command line and if you still get an error, it's a problem w/ Excel/Microsoft it would seem.
KRUNAL
on 25 Jul 2014
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
0 votes
1 Comment
dpb
on 28 Jul 2014
That undoubtedly would be thru active-x interaction and that level of dealing with Excel is beyond my pay grade...I don't use Excel. So, maybe somebody else will see this or start a new thread w/ that question or check on an Excel group for the actual stuff you need as it really isn't a Matlab question, per se...
KRUNAL
on 28 Jul 2014
0 votes
1 Comment
dpb
on 28 Jul 2014
You can put anything in the GUI callbacks you want, so "yes" it can be done. It'll still be ActiveX interacting w/ Excel, however, and on that I'm no help; you can read the interminable doc on methods, etc., as easily as I as I'd be starting from dead zero, too.
KRUNAL
on 28 Jul 2014
0 votes
KRUNAL
on 5 Aug 2014
0 votes
4 Comments
dpb
on 5 Aug 2014
"IF", I'm sure you can. "How" is look up all the myriad of object properties and methods in the Excel documentation--I "know nuthink" of it, specifically, though, so I'd be at the same learning point as you as noted above.
KRUNAL
on 5 Aug 2014
dpb
on 5 Aug 2014
The doc's for Excel will be associated with Excel, not Matlab. It's not a Matlab question of how Excel methods/properties work; that's stuff MS supplies with Excel. Matlab simply uses the Windows OS stuff. Here's a link that looks like should be a decent starting point but as I say, I've never done enough of this to have more than the most cursory of actual knowledge--I had others I could hand this sort of coding if needed off to and I'm disinterested in learning more than nothing about the subject now. :)
KRUNAL
on 5 Aug 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')
Categories
Find more on Spreadsheets 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!