Running a code on multiple input files and get results
2 views (last 30 days)
Show older comments
Stefania Avvedimento
on 16 Mar 2021
Edited: Stefania Avvedimento
on 17 Mar 2021
Hi all,
find below the code I am using to analyze a hydraulic network (the toolkit I am using links the hydraulic software EPANET with Matlab provided):
clear; close('all'); clc;
start_toolkit;
d = epanet('BWSN_flush1(0-1).inp'); %load the network
basedemand=d.getNodeBaseDemands;
idx = find(basedemand{1,1}~= 0);
nnodes=numel(idx)
qual_res = d.getComputedQualityTimeSeries;
qual_res.NodeQuality(~mod(qual_res.Time,3600)==0,:)=[];
T=0:1:240;
for i=1:nnodes
qual=qual_res.NodeQuality(:,idx)';
res=[[NaN T]; [idx'-1 qual]];
end
xlswrite('flush1(0-1)',res) %write results
Since I have to run this code several times (e.g. BWSN_flush1(1-2),BWSN_flush1(2-3),BWSN_flush1(3-4),BWSN_flush2(0-1) and others), is there any way to automate the code so that for each file input file.inp it gives me a file.xls (it would be great if this file gets the same name of the .inp one).
So at this time I have to run the following inp files:
BWSN_flush1(0-1), BWSN_flush1(1-2), BWSN_flush1(3-4), BWSN_flush1(5-6) , BWSN_flush1(7-8) BWSN_flush1(9-10) BWSN_flush1(11-12) BWSN_flush1(12-13) BWSN_flush1(13-14) BWSN_flush1(14-15) BWSN_flush1(15-16) BWSN_flush1(16-17) BWSN_flush1(17-18) BWSN_flush1(18-19) BWSN_flush1(19-20) BWSN_flush1(20-21) BWSN_flush1(21-22) BWSN_flush1(222-23) BWSN_flush1(23-24) BWSN_flush2(0-1) BWSN_flush2(0-1) BWSN_flush2(0-1) BWSN_flush2(0-1) BWSN_flush2(0-1) BWSN_flush2(0-1) BWSN_flush2(0-1) BWSN_flush2(0-1) BWSN_flush2(0-1) BWSN_flush2(1-2) BWSN_flush2(2-3) BWSN_flush2(3-4) BWSN_flush2(5-6) BWSN_flush2(6-7) BWSN_flush2(7-8) BWSN_flush2(8-9) BWSN_flush2(9-10) BWSN_flush2(10-11) BWSN_flush2(11-12).
Thanks,
Stefania
0 Comments
Accepted Answer
ANKUR KUMAR
on 16 Mar 2021
Edited: ANKUR KUMAR
on 16 Mar 2021
clc
clear
F=dir('BWSN_flus*inp')
for kk=1:length(F)
filename=F(kk).name
% DO YOUR CALCULATIONS
outputfilename=strsplit(filename,'.');
excelfileoutputfilename=strcat(outputfilename{1},'.xlsx')
end
Below is the complete code (not verified, as you have not provided the sample data):
clc
clear
F=dir('BWSN_flus*inp')
for kk=1:length(F)
filename=F(kk).name
d = epanet(filename); %load the network
basedemand=d.getNodeBaseDemands;
idx = find(basedemand{1,1}~= 0);
nnodes=numel(idx)
qual_res = d.getComputedQualityTimeSeries;
qual_res.NodeQuality(~mod(qual_res.Time,3600)==0,:)=[];
T=0:1:240;
for i=1:nnodes
qual=qual_res.NodeQuality(:,idx)';
res=[[NaN T]; [idx'-1 qual]];
end
outputfilename=strsplit(filename,'.');
excelfileoutputfilename=strcat(outputfilename{1},'.xlsx')
xlswrite(excelfileoutputfilename,res) %write results
end
1 Comment
More Answers (0)
See Also
Categories
Find more on Logical 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!