How to interface PHREEQC with MATLAB?

36 views (last 30 days)
How to interface PHREEQC with MATLAB?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 20 May 2019
Edited: MathWorks Support Team on 20 May 2019
In order to use PHREEQC with MATLAB, please install one of the 'IPhreeqcCOM' modules which provides a COM-Server-Interface to 'PHREEQC'. This is available on:
In the attachments you can find a Live Script which provides a full example to interface the PHREEQC software using the ActiveX interface.
To create an ActiveX server, the following was defined in the code, please change the path so it fits to your installation.:
%Creation of the ActiveX server:
iphreeqc = actxserver('IPhreeqcCOM.Object');
%Specify the database file according to the location on your file system.
iphreeqc.LoadDatabase('C:\Program Files\USGS\IPhreeqcCOM 3.5.0-14000\database\wateq4f.dat'); %Please change this line if you installed this somewhere else
%Call PhreeqC functionality using separate input file
%Note that in this way dynamic assignment of values to variables is not possible.
%I assume that the input file for PhreeqC is located in the current working directory.
OUTphreeqFILE = iphreeqc.GetSelectedOutputArray
%Call PhreeqC functionality providing input as MATLAB multiline String
%Define Aqueous Solution
cca=10; %Conc. of Ca2+ (mmol/l)
cmg=50; %Conc. of Mg2+ (mmol/l)
%Define input string as cell array of strings and combine them to a multiline string using the sprintf command.
IPCstringCell= {'SOLUTION 1', ...
'-temp 10', ...
'-units mmol/L', ...
['Ca ', num2str(cca)], ...
['Mg ', num2str(cmg)], ...
'Na 550', ...
'K 10', ...
'Cl 470', ...
'S(6) 28', ...
'C(4) 2.1', ...
'Alkalinity 2.3', ...
'-saturation_indices Calcite Dolomite', ...
'-molalities CO2 HCO3-', ...
'soln false', ...
'pH false', ...
'sim false', ...
'state false', ...
'time false', ...
'step false', ...
'pe false', ...
'distance false'};
IPCstring = sprintf('%s\n', IPCstringCell{:})
%Use the PhreeqC object method RunString with the above defined multiline string.
iphreeqc.RunString( IPCstring );
OUTphreeqSTRING = iphreeqc.GetSelectedOutputArray
The interface which is defined in the script can be improved by the development of a PHREEQC-class in MATLAB. There you could implement the 'actxserver' and 'iphreeqc.LoadDatabase' into the class constructor.
If you need more general information about ActiveX interfaces in MATLAB, please see the following site in our documentation:
  1 Comment
Ahmadreza shojaee
Ahmadreza shojaee on 22 Apr 2021
Thanks a lot.
Your answer helped me a lot.
Good luck!

Sign in to comment.

More Answers (2)

How to Couple Iphreeqc with Matlab. Please give me a simple example for better understanding.

Ehsan on 27 Sep 2021




Community Treasure Hunt

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

Start Hunting!