<div dir="ltr">Hi all,<div>After receiving very useful feedback from the SPICE team, I was able to solve the issues I was running into and integrate cspice within my MEX model.<br>A full example can be found here: <a href="https://urldefense.us/v3/__https://github.com/EllissoideRotondo/mex_cspice_wrapper_example__;!!PvBDto6Hs4WbVuu7!YrD03VH5oZcfwDu-TMtyN2C3ZTI-iZIw_jWjZnUSv0K6j0h3xqxFRbw8yOyQR4AIjG0WRFikIx4dOQ$">https://github.com/EllissoideRotondo/mex_cspice_wrapper_example</a>.<br><br>Some important takeaways:<br>- To avoid crashes, it is necessary to call the cspice function erract_c and set it to "RETURN" before any other cspice calls, which prevents cspice from throwing an exception. This only needs to be called once per program run.<br><br>- It is a good idea to call errdev_c and set the output device to NULL, which prevents cspice from trying to dump errors to stdout. This also needs to be run only once.<br><br>- Any exceptions encountered by cspice can be captured using the cspice functions failed_c (check if any error occured), getmsg_c (get the error message), and reset_c (reset the error status). Note that reset_c must be called any time an exception occurs, otherwise cspice will keep returning an error on subsequent calls.<br>Once captured, a Matlab exception can be thrown using the error command (in MEX, you will need to define coder.exstrisinc('error'), as the command is not natively compatible with code generation).<br><br>- As a final note, I also recommend upgrading to the latest version of Matlab (R2022a) if possible. There is a bug in earlier versions that occasionally causes Matlab to crash when running certain MEX files in debug mode.<br><br>Thanks,<br>Alex Pascarella<br><br><br></div></div>