matlab-upgrade-mex-ic

Convert C, C++, and Fortran MEX files from Separate Complex to Interleaved Complex API.

995|122|Updated Apr 3, 2026
One-click install
npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-upgrade-mex-ic
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: matlab-upgrade-mex-ic
Source: https://github.com/matlab/matlab-agentic-toolkit/tree/main/skills-catalog/matlab-external-language-interfaces/matlab-upgrade-mex-ic
Command: npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-upgrade-mex-ic

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Legacy MEX files written with the Separate Complex API (mxGetPr/mxGetPi) fail to compile with mex -R2018a or silently return wrong results on complex arrays, and they incur O(n) deinterleave/re-interleave copy overhead on every call. This Skill guides a verified, step-by-step migration to the Interleaved Complex API.

Core Features & Use Cases

  • Guided 7-step migration workflow: Pre-upgrade analysis, output file decision, IC-only vs SC/IC guarded mode selection, iterative refactoring, build, verification, and documentation.
  • Complete API mapping tables: Required and recommended replacements for mxGetPr, mxGetPi, mxGetData, mxGetImagData, and type-specific accessors for all numeric types in C, C++, and Fortran.
  • SC/IC guarded builds: Patterns using #if MX_HAS_INTERLEAVED_COMPLEX so one source compiles under both mex -R2017b and mex -R2018a, including Fortran .F preprocessor requirements.
  • Performance quantification: Benchmark template and guidance showing how IC conversion eliminates boundary copy overhead for large complex arrays.
  • Use Case: A user has a legacy Fortran MEX file from File Exchange using mxGetPr/mxGetPi via %val() pointers. The Skill converts it to an SC/IC guarded .F file, provides build commands for all three modes, and supplies assertion-based tests comparing old and new outputs.

Quick Start

Convert my legacy MEX file filter.c from the separate complex API to the interleaved complex API and verify the outputs match.

Frequently Asked Questions about matlab-upgrade-mex-ic

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I convert a MEX file from separate complex to interleaved complex API?

Replace mxGetPr/mxGetPi with mxGetComplexDoubles and access .real/.imag fields, use mxIsComplex for complexity checks, and build with mex -R2018a. The Skill walks through analysis, refactoring, build, and verification steps in order.

How do I make one MEX source compile with both -R2017b and -R2018a?

Wrap the code in #if MX_HAS_INTERLEAVED_COMPLEX / #else preprocessor guards so the compiler selects the IC or SC path at build time. For Fortran, the file must use an uppercase .F or .F90 extension so the preprocessor runs.

Why does mxGetPr give wrong results on complex arrays in IC mode?

In IC mode, mxGetPr on a complex array returns a pointer to interleaved real/imaginary data, so iterating it as real-only silently produces wrong values. Use mxGetComplexDoubles and access the .real and .imag struct fields instead.

Does interleaved complex conversion improve MEX performance?

Yes, for complex data. SC MEX calls force MATLAB to deinterleave inputs and re-interleave outputs, an O(n) copy of about 16 bytes per element per complex array. IC MEX functions access the native layout with zero boundary copy, which matters most for large arrays or frequent calls.

Can Fortran .f90 MEX files use MX_HAS_INTERLEAVED_COMPLEX guards?

No. Lowercase .f and .f90 extensions skip the C preprocessor, so #if guards cause syntax errors. Rename the file to .F or .F90 for guarded builds, or use an IC-only conversion without guards.

When should I not use this MEX conversion skill?

Skip it when writing a new MEX function from scratch, when using the C++ MEX API (matlab::mex::Function), or when the file already uses typed accessors like mxGetDoubles and mxGetComplexDoubles, since it is already IC-compatible.