Main Content

mexhost

Create host process for C++ MEX function

Description

mh = mexhost creates a MEX host process that is used to run C++ MEX functions. The default process name is MATLABMexHost.

Use the feval method of the matlab.mex.MexHost object returned to execute C++ MEX functions in the MEX host process.

example

mh = mexhost("EnvironmentVariables",envVariables) sets environment variable with values defined in envVariables for the process.

example

Examples

collapse all

Create a host process and run a C++ MEX function in that process.

The arrayProduct.cpp C++ MEX file contains the source code for a function that multiplies an array by a scalar input and returns the resulting array. Open this file and save it on your MATLAB® path. Build the C++ MEX source file using the mex command. To set up the MEX build, follow the instructions in Build C++ MEX Programs.

mex arrayProduct.cpp

Create a host process. The mexhost function returns a matlab.mex.MexHost object.

mh = mexhost;

Use the feval method of the matlab.mex.MexHost object to evaluate the C++ MEX function in the host process.

result = feval(mh,"arrayProduct",10,[2,4,6,8])
result =

    20    40    60    80

You can use the MexHost object to find the identifier of the process created by the mexhost function.

mh = mexhost;
mh.ProcessIdentifier
ans = 
    "13336"

Each call to mexhost creates a process.

Set the value of environment variable envName1 to envVal1 and the value of variable envName2 to envVal2.

s = ["envName1","envVal1"
     "envName2","envVal2"];
mh = mexhost("EnvironmentVariables",s)
mh = 

  MexHost with properties:

             ProcessName: "MATLABMexHost"
       ProcessIdentifier: "19344"
               Functions: [0×0 string]
    EnvironmentVariables: "envName1"    "envVal1"
                          "envName2"    "envVal2"

Input Arguments

collapse all

Environment variables and values, specified as an n-by-2 string array. Non-ASCII characters are not supported. The first column is the name of the environment variable and the second column is the value.

The function prepends the environment value to the environment variable if the variable name is:

  • PATH on Windows® platforms

  • LD_LIBRARY_PATH on Linux® platforms

  • DYLD_LIBRARY_PATH on macOS platforms

Example: mh = mexhost("EnvironmentVariables",["path","c:\bin\mylib\"])

Output Arguments

collapse all

Host process, returned as a matlab.mex.MexHost object. Use this process to run a C++ MEX function outside of the MATLAB process.

More About

collapse all

Version History

Introduced in R2019a