MATLAB Answers

How to use MATLAB coder when .m file calls a MEX file?

27 views (last 30 days)
Hi all,
To speed up my simulations, I am trying to convert my Matlab files to MEX files. Since I have only started working with MEX files recently, I am not quite sure about the following:
I have a function restr() which calls another function interp() that I have converted to a MEX file earlier:
function [R] = restr()
[I] = interp_mex();
R = I';
% Followed by some additional code that modifies R
end
When I try to convert this function using MATLAB coder, I get the error message that "only MATLAB files are supported for code generation". My questions now are:
  1. Of course I can use my non-converted function interp() instead of interp_mex(). When I use MATLAB coder to convert restr(), does it also convert the functions that are called within the file? So, is interp() also converted?
  2. If not, how can I convert MATLAB files that use MEX files?
Thank you for your time!
Kind regards, Quinten

  0 Comments

Sign in to comment.

Accepted Answer

OCDER
OCDER on 6 Jul 2018
Of course I can use my non-converted function interp() instead of interp_mex(). When I use MATLAB coder to convert restr(), does it also convert the functions that are called within the file? So, is interp() also converted?
Yes, you should use the non-converted function interp() instead of interp_mex(). Yes, MATLAB coder will attempt to convert all dependent function, so restr() and interp() should be converted.
If not, how can I convert MATLAB files that use MEX files?
You can't, as Matlab Coder does not have a way to translate custom MEX files, and also, it's odd to do since it'll be like converting a c++ code to a c++ code.
If you have a lot of functions you want as MEX, then convert them all at once as purely matlab codes. If you want to build custom MEX file and you have a ton of reusable codes, then you could build a common c++ source code library, make c++ header file, and then use #include mylibrary.h in your MEX source codes.
//contents of myMEX.cpp
#include "mex.h"
#include "mylibrary.h"
void mexFunction(.....) {
... your code
}
//contents of mylibrary.h
#ifndef MYLIBRARY_H
#define MYLIBRARY_H
int add(int, int);
int subtract(int, int);
#endif
//contents of mylibrary.cpp
int add(int x, int y) { return (x + y); }
int subtract(int x, int y) { return (x - y); }
To compile, you'll need something like this
mex myMEX.cpp mylibrary.cpp

  1 Comment

Quinten Rensen
Quinten Rensen on 7 Jul 2018
Thank you for your quick respond! I will work on it.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!