There are several options to use EPANET from MATLAB. MATLAB is a numerical computing environment and programming language.

  • The Open Water Analytics projects provides instructions and sample code to use EPANET and EPANET-MSX from MATLAB. Source code and information are available on Github: epanet-matlab.
  • Marios Kyriakou and Demetrios Eliades provide an EPANET-Matlab Class.
  • The EPANET Toolkit DLL can be called directly using MATLAB's loadlibrary() and callib() functions. A minimalistic example is given below.

Choosing the right DLL version (64 or 32bit)

It is important that you find a DLL version that is compatible with your MATLAB version. E.g. a 32bit EPANET Toolkit DLL will not work with the 64bit version of MATLAB. To further complicate things, not only the architecture has to be right, but the EPANET Toolkit also needs to be linked to the same C Runtime as MATLAB. Thus different MATLAB versions can require different versions of the EPANET Toolkit. You can find different versions of the EPANET Toolkit DLL on the following pages:

If none of the above mentioned DLLs work for you, you should compile the DLL yourself. The MATLAB support pages explain which compilers are supported: Supported compilers for MATLAB R2013b The OpenWaterAnalytics/epanet repository contains build files for many compilers.

Once you've successfully compiled your Epanet Toolkit DLL, please consider sharing it on the internet to help other users.

MATLAB example code calling the EPANET Toolkit DLL

To run the snippet below, you'll need

  • An epanet2.dll that is compatible with your MATLAB version (see above)
  • epanet2.h aka toolkit.h
  • A sample INP file like Net1.inp
% epanet2.dll and epanet2.h have to be in the current working directory 

% Net1.inp has to be in the current working directory
% You have to be able to write Net1.txt and Net1.bin 
[errorcode] = calllib('epanet2','ENopen','Net1.inp','Net1.txt','Net1.bin');
if errorcode
  disp(sprintf('ENopen() returned %d', errorcode))

% Solve hydraulics for all time periods
[errorcode] = calllib('epanet2', ENsolveH');
if errorcode
  disp(sprintf('ENsolveH() returned %d', errorcode))

% Retrieve pressure of the first node in the last timestep
pressure = 0.0;
[errorcode] = calllib('epanet2', 'ENgetnodevalue', 1, EN_PRESSURE, pressure);
if errorcode
  disp(sprintf('ENgetnodevalue() returned %d', errorcode))
  disp(sprintf('Pressure at the first node, last timestep: %f', pressure))