MATLAB Wrapper

The only interface for MATLAB that is supported as of version 6.2 is via Python (you can also call the DLL directly, see below). The maintenance hurdles of supporting the old SWIG MATLAB wrapper proved to be too difficult to surmount.

Via Python

You will need to acquire a Python interpreter, the easiest method to do so if you do not already have Python installed on your computer is to download the installer from python.org: link to downloads. There is also information on the Mathworks website. The Python wrapper only uses methods that are in the Python standard library, so the standard installation of Python would work fine. If you would like to have a more full-featured Python installation, you can install a full-fledged Python installation from Anaconda: Anaconda download.

In your MATLAB shell, you can inquire about what Python version MATLAB intends to use with a command like:

>> pyversion

       version: '3.6'
    executable: 'D:\Anaconda\python.exe'
       library: 'D:\Anaconda\python36.dll'
          home: 'D:\Anaconda'
      isloaded: 0

Good. It found Python, and has not loaded it yet. You are ready!

If you have multiple copies of Python on your computer already, then you can tell MATLAB which one you want it to use by passing the absolute path to the python executable to pyversion. For instance:

>> pyversion d:\Anaconda\envs\py36\python.exe
>> pyversion

       version: '3.6'
    executable: 'd:\Anaconda\envs\py36\python.exe'
       library: 'd:\Anaconda\envs\py36\python36.dll'
          home: 'd:\Anaconda\envs\py36'
      isloaded: 0

Finally, you need to install CoolProp into your given copy of python. This one-liner calls the pip program of Python to install the CoolProp package from the PYPI package index. Watch out for the spaces in the arguments, they are important!:

>> [v,e] = pyversion; system([e,' -m pip install --user -U CoolProp'])

Use

Then you can calculate the normal boiling point temperature of water:

>> py.CoolProp.CoolProp.PropsSI('T','P',101325,'Q',0,'Water')

ans =

  373.1243

Similar approaches are possible for the low-level interface. Addition of docs documenting how to use the low-level interface in MATLAB would be welcome. Also, more advanced wrappers for MATLAB are available, and are stored on github: https://github.com/CoolProp/CoolProp/tree/master/wrappers/MATLAB

Calling Low-Level interface through DLL

%% Calling low-level interface from MATLAB through shared library (DLL)
%%
%% Originally developed by Edo Macchi, modified by Ian Bell
%% 
%% December 2015

path_to_lib = '/work/CoolProp/libs/shared'; %specify path to coolprop shared library
path_to_include= '/work/CoolProp/include'; %specify path to coolprop's include folder

% Loading shared library
if ~libisloaded('coolprop') %checking whether library is already loaded
    addpath(path_to_lib)
    addpath(path_to_include)
    libname = 'libCoolProp' % OSX and linux
    if ispc
        libname = 'CoolProp'
    end
    loadlibrary(libname,'CoolPropLib.h','includepath',path_to_include,'alias','coolprop'); % loading library with alias coolprop
    disp('loaded CoolProp shared library.')
    disp('loaded these functions: ')
    libfunctions coolprop
end

buffer_size = 1000;
ierr = 0;
b = (1:1:buffer_size);
herr= char(b);

%Selecting backend and fluid
backend = 'BICUBIC&HEOS';
fluid = 'Water';
[handle, sh] = calllib('coolprop','AbstractState_factory',backend,fluid,ierr,herr,buffer_size);

length=100000;
input1 = linspace(700000.0,1500000.0,length)';
input2 = linspace(2.8e6,3.0e6,length);
[input_pair,sip] = calllib('coolprop','get_input_pair_index','HmassP_INPUTS');

tic;

%Creating input and output pointers
input1Ptr = libpointer('doublePtr',input1);
input2Ptr = libpointer('doublePtr',input2);
TPtr = libpointer('doublePtr',zeros(length,1));
pPtr = libpointer('doublePtr',zeros(length,1));
rhomolarPtr = libpointer('doublePtr',zeros(length,1));
hmolarPtr = libpointer('doublePtr',zeros(length,1));
smolarPtr = libpointer('doublePtr',zeros(length,1));

calllib('coolprop','AbstractState_update_and_common_out',handle,input_pair,input1Ptr,input2Ptr,length,TPtr,pPtr,rhomolarPtr,hmolarPtr,smolarPtr,ierr,herr,buffer_size);

dt = toc;

T=get(TPtr,'Value');
p=get(pPtr,'Value');
rhomolar=get(rhomolarPtr,'Value');
hmolar=get(hmolarPtr,'Value');
smolar=get(smolarPtr,'Value');

fprintf('%d us/call\n', (dt*1e6/length));

outputs=zeros(5,1);
%Choosing parameters to compute
[outputs(1,1), so1] = calllib('coolprop','get_param_index','T');
[outputs(2,1), so2] = calllib('coolprop','get_param_index','P');
[outputs(3,1), so3] = calllib('coolprop','get_param_index','Dmolar');
[outputs(4,1), so4] = calllib('coolprop','get_param_index','Hmolar');
[outputs(5,1), so5] = calllib('coolprop','get_param_index','Smolar');

%Creating output pointers
out1Ptr = libpointer('doublePtr',zeros(length,1));
out2Ptr = libpointer('doublePtr',zeros(length,1));
out3Ptr = libpointer('doublePtr',zeros(length,1));
out4Ptr = libpointer('doublePtr',zeros(length,1));
out5Ptr = libpointer('doublePtr',zeros(length,1));

tic;

calllib('coolprop','AbstractState_update_and_5_out',handle,input_pair,input1Ptr,input2Ptr,length,outputs,out1Ptr,out2Ptr,out3Ptr,out4Ptr,out5Ptr,ierr,herr,buffer_size);

dt=toc;

%Saving computed values to array
out1=get(out1Ptr,'Value');
out2=get(out2Ptr,'Value');
out3=get(out3Ptr,'Value');
out4=get(out4Ptr,'Value');
out5=get(out5Ptr,'Value');

fprintf('%d us/call\n', (dt*1e6/length));