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

조회 수: 12 (최근 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
Jonathan Bessette 2023년 5월 7일
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