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

10 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2023-5-7
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 个评论
Jonathan Bessette
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by