Borrar filtros
Borrar filtros

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

10 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 7 de Mayo de 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 comentario
Jonathan Bessette
Jonathan Bessette el 7 de Mayo de 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!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Simulink en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by