o
odinpkg.dev
packages / app / odin_mex_matlab_extension

odin_mex_matlab_extension

16c90e8app

Demos how to build a Matlab mex file using the Odin language (https://odin-lang.org/)

No license · updated 2 months ago

Building a Matlab mex file using Odin

Example of how to build a Matlab mex file using the Odin programming language.

Tested in Matlab r2025b on Windows (11) & Linux Mint (22.1), but I think it'll work with Matlab versions starting from 2018 when the interleaved complex array api was introduced.

addemup.odin contains the required mexFunction() entry point (it just adds 2 identically-sized double real arrays & returns the result), the other .odin files define the Matlab library interfaces & were generated using odin-c-bindgen & very slightly tweaked (turns out the functions are all defined in libmx, although they're declared in the matrix.h header).

Run the bindit.m Matlab script to build the mex file, it:

  1. Generates a file called mex_version.odin containing the required mexfilerequiredapiversion() function. The API version is hard-coded as 800 (the aforementioned version with interleaved complex arrays), the Matlab version is extracted from the version() function. This function is normally defined in MATLAB/{version}/extern/version/c_mexapi_version.c

  2. Invokes odin build . (with some additional arguments) to compile the mex file.