Read and write multiple excel files.one after the other
4 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
採用された回答
dpb
2014 年 7 月 24 日
編集済み: dpb
2014 年 7 月 24 日
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 件のコメント
dpb
2014 年 7 月 24 日
What ever you want to do with the data you're read in...cut 'n paste what you want from your other scripts into the general outline provided. That was the place to do whatever calculations you want with the two data sets as the comment says and whatever else is to be done with the two files' individual datasets.
その他の回答 (8 件)
dpb
2014 年 7 月 23 日
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...
KRUNAL
2014 年 7 月 24 日
編集済み: KRUNAL
2014 年 7 月 24 日
4 件のコメント
dpb
2014 年 7 月 25 日
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
2014 年 7 月 28 日
1 件のコメント
dpb
2014 年 7 月 28 日
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
2014 年 7 月 28 日
1 件のコメント
dpb
2014 年 7 月 28 日
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
2014 年 8 月 5 日
4 件のコメント
dpb
2014 年 8 月 5 日
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. :)
Tony Castillo
2019 年 11 月 7 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!