Clear Filters
Clear Filters

How to bring in pre-built or pre-made functions from MATLAB to Simulink without rewriting them into new "MATLAB function" blocks?

10 views (last 30 days)
Hey folks, say I have a function in MATLAB that I wrote
for example: "add_two_numbers.m" which I attached,
And I want to place it in Simulink as a block (in this simplified example case, [x,y] input and [z] output) in a model, e.g., "Model.slx".
I see that "MATLAB Function" exists in Simulink (see attached image), however, I don't want to have to copy and paste every new function I make and save it as a new function block in Simulink. To elaborate, this information https://www.mathworks.com/help/simulink/slref/matlabfunction.html is unsatisfying because they write an entirely new function in the block, rather than referencing something they've already edited and created.
This may be an obvious question that I missed in documentation, but is there any way to directly call in Simulink, a function I previously made in MATLAB? I wish I could just drag and drop the function from my path and have it pop up in Simulink. I'd love to be able to edit the function in MATLAB's editor and then use it in Simulink, and have this function in Simulink auto-update / be linked to the MATLAB function.
Let me know! Thanks so much,
Jon

Accepted Answer

Walter Roberson
Walter Roberson on 7 May 2023
is there any way to directly call in Simulink, a function I previously made in MATLAB?
No, not as far as I can tell.
You can write Level 2 S functions in MATLAB, https://www.mathworks.com/help/simulink/sfg/writing-level-2-matlab-s-functions.html but that is non-trivial work.
Sometimes the easiest thing to do is to write a "shim" -- a (Simulink) MATLAB Function Block that has a few lines to establish size and types of variables, and then calls your MATLAB function to do the real work.
  1 Comment
Jonathan Bessette
Jonathan Bessette on 7 May 2023
Bummer, I appreciate the response though.
I do like the idea of a simple "shim" function block that just calls the desired MATLAB function, I think that's what I'll try and do.
Thanks again!

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!