Hi @Shalaka, to use the output of your MEX function in a Simulink model and assign it to variables in the MATLAB workspace, there are two main approaches, depending on how your MEX function is structured.Option 1: Use a MATLAB Function Block (if the MEX behaves like a regular MATLAB function) If your MEX file is callable directly from MATLAB using a syntax like:
result = myMexFunction();
then you can follow these steps:
- In your Simulink model, insert a MATLAB Function block.
- Double-click the block and write a wrapper like:
Option 2: Create a C-based S-Function (if the MEX is low-level or incompatible with MATLAB syntax)
If your MEX function isn't directly usable in MATLAB (e.g., it returns a struct or requires complex pointers), you can wrap the C code into a Simulink-compatible S-function:
- Write an S-function in C that calls your Sfty_GetReason() function inside mdlOutputs.
- Compile it with mex in MATLAB:
- Add an S-Function block to your model and specify the compiled .mex file.
- Connect the output to a To Workspace block to log values during simulation.
Hope this helps!