Mex Funcompk Access

Mex Funcompk Access

Mex Funcompk Access

If funcompk is a computationally intensive MATLAB function, converting it to a MEX file can significantly speed up execution or enable external applications to run it without a MATLAB license (with the MATLAB Runtime).


If you want to run a mex funcompk analysis, you will likely need specific software environments. These are not found in standard Excel or SPSS.

The convergence of MEX (Model Execution) and FunCompPK (Functional Comparative PK) is paving the way for Virtual Twins in pharmacology.

Soon, every new drug application may include a digital file containing a full FunCompPK analysis. Regulators will run that file against their own MEX engine to verify results instantly. mex funcompk

Problem: "Error: Cannot allocate vector of size X GB." Solution: FunCompPK creates large distance matrices (comparing every curve to every other curve). Reduce the number of time points via interpolation (downsample to 100 time points) before running the comparison.

fit <- nlme(conc ~ SSfol(Dose, time, lKe, lKa, lCl), data = pk_data, fixed = lKe + lKa + lCl ~ 1, random = pdDiag(lKe + lKa + lCl ~ 1), groups = ~ Subject)

Imagine your funcompk takes time points, dose, and parameters (Vd, Cl, Ka, etc.) and returns concentrations. If funcompk is a computationally intensive MATLAB function,

Step 1: Write the C code (snippet for a 1‑compartment oral model)

// funcompk.c
#include "mex.h"
#include <math.h>

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) double *t, *params, *conc; size_t n; double Vd, Cl, Ka, dose; double k10 = Cl / Vd;

// Get inputs
t = mxGetPr(prhs[0]);   // time vector
n = mxGetN(prhs[0]);
params = mxGetPr(prhs[1]); // [Vd, Cl, Ka, dose]
Vd = params[0]; Cl = params[1]; Ka = params[2]; dose = params[3];
// Output array
plhs[0] = mxCreateDoubleMatrix(1, n, mxREAL);
conc = mxGetPr(plhs[0]);
// Compute concentration at each time point
for (int i = 0; i < n; i++) 
    double t_val = t[i];
    if (t_val < 0) conc[i] = 0;
    else conc[i] = (dose * Ka / (Vd * (Ka - k10))) * (exp(-k10 * t_val) - exp(-Ka * t_val));

Step 2: Compile in MATLAB

mex funcompk.c

Step 3: Call it like any MATLAB function If you want to run a mex funcompk

t = 0:0.1:24;
params = [25, 5, 1.2, 100]; % Vd, Cl, Ka, dose
C = funcompk(t, params);
plot(t, C);
Page Top