**Executing an EES Macro File from MATLAB**

EES can be started and directed to run a predefined macro file from most programs. This capability allows other programs to communicate with EES and direct its execution. To illustrate this capability, we will develop a MATLAB function that returns the specific volume, specific enthalpy, and specific entropy given the fluid, temperature, and pressure.

The MATLAB function is called EESProperty; the header is given below:

function[v, h, s] = EESProperty(R, T, P)

%[v,h,s]=EESProperty(R,T,P)

%

% Inputs

% R = an EES recognized fluid

% T = temperature (C)

% P = pressure (kPa)

%

% Outputs

% v = specific volume (m^3/kg)

% h = specific enthalpy (J/kg)

% s = specific entropy (J/kg-K)

end

The file C:\Temp\Input.dat is opened using the fopen command in MATLAB. Note the 'w' flag indicates that the file is available to write to and any existing contents are flushed. The fprintf command is used to write the inputs R, T, and P to the file. Notice the formatting required to put single quotes around the string R so that it is read appropriately as a string in EES. The file is closed using the fclose command.

fid=fopen('C:\Temp\Input.dat','w'); %open the input file for writing

fprintf(fid,'''%s'' %f %f',R,T,P); %write the inputs to the file

fclose(fid); %close the file

The system command in MATLAB executes a command in the operating system as if it were entered into the Run Window. We will use the system command to open EES with the macro file name as an input parameter.

system('C:\EES_CAE\EES.exe C:\Temp\aMacro.emf'); %run the macro file

EES will open and automatically execute the commands in the macro C:\Temp\aMacro.emf. A listing of aMacro.emf follows.

Open 'C:\Temp\TestMacro.ees'

Units SI C kPa J Mass

Import 'C:\Temp\Input.dat' R$ T P

Solve

Export 'C:\Temp\Output.dat' R$ T P v h s

Quit

The EES file that is opened in the macro file contains the following EES code.

v=volume(R$,T=T,P=P)

h=enthalpy(R$, T=T,P=P)

s=entropy(R$, T=T,P=P)

Executing the system command in MATLAB causes EES to open the file C:\Temp\Input.dat and assign the contents to the variables R$, T, and P in the EES file. The file is solved and the properties v, h, and s are written to the file C:\Temp\Output.dat. Finally, EES is closed. All that remains is to open the file C:\Temp\Output.dat and assign the values to the output parameters v, h, and s in the MATLAB function EESProperty. This is done using the importdata command in MATLAB.

TPvhs=importdata('C:\Temp\Output.dat'); %import results from the file

v=TPvhs.data(3); %assign the results to outputs

h=TPvhs.data(4);

s=TPvhs.data(5);

The MATLAB function EESProperty can now be run like any other MATLAB function providing access to EES' internal property routines. To calculate the properties of R134a at 50ºC and 200 kPa, enter the following lines in the MATLAB Command Window:

>> R='R134a';T=50;P=200;

>> [v,h,s]=EESProperty(R,T,P)

v =

0.1277

h =

2.9670e+005

s =

1.1164e+003

which leads to *v* = 0.1277 m3/kg, *h* = 2.9670x105 J/kg, and *s* = 1.1164 J/kg-K. Notice that there is no visual indication that EES has started and executed the macro command list. However, you see that the C:\Temp\EESMacro.log file appears in the directory.

See also: