Running a code on multiple input files and get results

조회 수: 3 (최근 30일)
Stefania Avvedimento
Stefania Avvedimento 2021년 3월 16일
편집: Stefania Avvedimento 2021년 3월 17일
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

채택된 답변

ANKUR KUMAR
ANKUR KUMAR 2021년 3월 16일
편집: ANKUR KUMAR 2021년 3월 16일
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
Stefania Avvedimento
Stefania Avvedimento 2021년 3월 17일
편집: Stefania Avvedimento 2021년 3월 17일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by