How can I call an m file in Simulink and put it to a block in my model (without using an S function)? Does anybody have an idea? I'd really appreciate it.
2 Answers
If you're trying to apply a user-defined MATLAB function to Simulink signals, there are a few different ways to do this, depending on your objective. All options are available within the User-Defined Functions section of the Simulink library.
Use the MATLAB function block if you intend to generate code from your model. This block does have restrictions, the entire gamut of built-in MATLAB functions is not available.
Use the Interpreted MATLAB function block if you don't care about code generation, this block can make use of any functions.
Use the Fcn block if your m-file is trivial and contains a simple expression operating on the inputs. In this case you can type the expression directly into the block dialog and reference the input / output signals as shown in the documentation.

- 106,671
- 19
- 240
- 328
-
When would you use S functions? I know people use them, but what are their advantages? Thanks for the big picture by the way. – KAE Apr 26 '12 at 13:53
-
@KE. I'm not sure when you would pick an m-file S-function over one of the approaches listed above. But if you mean C S-functions, those mostly come in handy when you you're dealing with code generation. In that case you'd create a non-inlined S-function that models simulation behavior as you want it to, and then create the parts that go into generated code in TLC. You can also create an inlined S-function that shares the simulation and code generation code, but I've found these to be of limited use. Also, C S-functions can be significantly faster than m-file S-functions and the options above. – Praetorian Apr 26 '12 at 15:40
-
MATLAB Fcn block is the best solution to embed M-function file into Simulink model. However, be cautious which version of MATLAB you are using, e.g., with later versions of MATLAB Function Block can be implemented with M-function file with %#codegen and C compiler need to be with your MATLAB package. Good luck

- 1