0

I have a code that uses optimvar and optimproblem functions that i have made in a .m file and that i want to recreate fur use in a simulink model to be used in a real time model.

Unfortunately the MATLAB Function block does not support these functions. Is there a way to get it to run them or alternatively, is there a way to call a .m file during the running of the simulink model? As far as i can tell the callback functions are only really useful for starting or ending a model, not running on each step.

4
  • There is a documentation page dedicated to "MATLAB function files in MATLAB function blocks", can you elaborate why you don't think that's possible? Are you getting a specific error? Commented Jun 25, 2024 at 13:24
  • The error I get is: "Function 'optimvar' not supported for code generation." and the same happens for optimproblem Commented Jun 25, 2024 at 13:39
  • Are you trying to run in rapid accelerator mode or in some other way compiling the model before running? What if you just run in "normal" mode so the model isn't compiled? I'm not sure code generation should be a prerequisite for running your model Commented Jun 25, 2024 at 14:32
  • This is with simulink running in normal mode. I have found a solution - apparently the function block runs a reduced version of matlab that doesnt have access to the full matlab library and these are some of the functions not included. So if you put: coder.extrinsic('my_fctn'); before you call the function it bypasses code generation and instead uses the base matlab engine to execute the call. Commented Jun 25, 2024 at 14:44

1 Answer 1

2

Use coder.extrinsic to declare the function as "external". There are a few limitations regarding the use of the output, see https://nl.mathworks.com/help/simulink/slref/coder.extrinsic.html?searchHighlight=coder.extrinsic&s_tid=srchtitle_support_results_1_coder.extrinsic

EDIT: For simulation you already found this out. ( Missed this the first time scanning the comments). You might want to try to make the code Coder-compatible. We have done that as well. Or just search for C/C++ implementations and integrate that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.