MATLAB: How to generate code for mex files too while generating hardware specific code from simulink

matlab codermex hardware-specific codesimulink

My simulink model contains a MATLAB function block which calls a precompiled mex file. I want to generate the code from this model for a specific hardware. But when I compile the generated C code, the line calling the mex file gives an error. I want to be able to generate c code without the error. Is there a way to do this?
Thanks.

Best Answer

Since MEX-files are specific to the MATLAB environment, you cannot run them on an embedded target. If you have the equivalent standalone C code equivalent also, you can use the following code style to call the MEX-file during simulation, and the C-code in the generated code:
function y = myfcn(u)
coder.extrinsic('mymex');
y = 0; %pre-allocate
if coder.target('Sfun')
y = mymex(u);
else
y = coder.ceval('foo', u);
end
Please refer to the documentation for more information on coder.target and coder.ceval.