Running a code on multiple input files and get results

3 views (last 30 days)
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

Accepted Answer

ANKUR KUMAR
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
Stefania Avvedimento
Stefania Avvedimento on 17 Mar 2021
Edited: Stefania Avvedimento on 17 Mar 2021
Thanks, it works! May I ask you another question?
In the excel files I get from the code, there are the node's concentration vs time (I send you an example) in a matrix. I'd like to extract nodes that have, let's say, from the 18th hour to the 24th hour a value below 0.2 and know the i th hour where the violation happens. I usually do it in Excel through the command count.if and then see the hours of violation manually (shown in red). I'd like to transfer these commands on my Matlab code, so after the violations are founded from a range a time (e.g. 18-24 h), get an index array of time violation.
I send you an example of what I do in Excel, the matrix, as it is, is obtained from Matlab code.
Could you please give me some advice?
Thanks,
Stefania

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!