MATLAB: How to pass data by reference to a function in Simulink

simulink

I have a custom C/C++ function which accepts a pointer as an input that I would like to include in my Simulink model. How can I implement this so that I can pass data by reference from Simulink?

Best Answer

One option for passing data by reference is to create an S-Function block for the custom C/C++ code which accepts pointers. To integrate pre-existing code, the Legacy Code Tool can be used to generate an S-Function block: 
An alternative approach is to call your C++ function from a MATLAB Function Block using "coder.ceval". You can then pass your inputs by reference using "coder.ref", "coder.rref", or "coder.wref". For example, the call to a C++ function "sort" within the MATLAB Function Block might look like:
coder.ceval('sort', coder.ref(myArray), arrayLength);
For more information on calling C/C++ code from a MATLAB Function block and the different options for passing data by reference, refer to the following documentation page: