mwArray vs MATLAB Data API - Which API should I use when using MATLAB Compiler SDK?

When developing libraries with MATLAB Compiler SDK, I see most deployment targets generally have two paths for handling data exchange: the legacy mwArray approach and the modern MATLAB Data API (MDA) approach. Which one should I use?

 Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team vor etwa 21 Stunden
Bearbeitet: MathWorks Support Team vor etwa 7 Stunden
For new development—especially if you want the best performance —we strongly recommend using MDA and particularly its strongly-typed approach.
Why choose MDA over mwArray?
mwArray was built on older technology and requires manual memory management and explicit data casting. In contrast, MDA (introduced for C++ in R2018a and expanded to .NET/Java/Python since) offers several modern advantages:
  • Zero/Minimal Data Copying: MDA uses a shared memory model with copy-on-write semantics. This significantly reduces the memory footprint and latency when passing large matrices between your application and the MATLAB Runtime.
  • Thread Safety: Unlike mwArray, MDA is designed for multi-threaded environments, allowing you to call compiled functions across different threads without complex locking.
  • Native Language Experience: Using the Strongly Typed approach with MDA, you don't need to wrap your data in MATLAB-specific classes like MWNumericArray. You simply pass native C++ vectors, C# arrays, or Java primitives.
  • Simplified Lifecycle: Modern APIs utilize smart pointers (C++) and native garbage collection (.NET/Java/Python) to handle the lifecycle of the MATLAB Runtime (and the libraries used with it) automatically.
Target Language
mwArray / MWArray
MDA
C++
Effort: Manual LibInitialize/Terminate and manual mwArray memory management.
Differences: requires deep copies for complex data; not thread-safe.
Effort: Automated lifecycle; uses standard C++11+ types via generateCPP.
Differences: Zero-copy/Copy-on-Write; thread-safe; full compile-time type checking; runtime instance can run in-process or out of process, applications can run synchronously or asynchronously.
C# / .NET
Effort: Heavy casting to MWArray types; must call .Dispose() on every array to avoid leaks.
Differences: marshaling overhead; type errors only caught at runtime.
Effort: Pass native types (double[], string) directly; memory managed by Garbage Collection.
Differences: IntelliSense support for method signatures; faster marshaling; asynchronous call support; runtime instance can run in-process or out of process, applications can run synchronously or asynchronously. Support for cross-platform (Windows, macOS, Linux) development and deployment.
Java
Effort: Explicitly wrap data in MWNumericArray etc.; requires manual .dispose() calls.
Differences: High object overhead; slower data transfer due to heavy wrapping.
Effort: Uses standard Java objects/primitives; lifecycle handled by JVM/Runtime.
Differences: Cleaner API; better memory efficiency; native support for structs and cells.
Python
Has always used MDA with all its associated benefits

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by