Search code examples
matlabsimulinks-function

call a level-2 sfunction via the matlab editor


I have developped a simulink model. I compiled it to have a level-2 sfunction : 'sfun.mexw32'. I would like to call this sfunction in a .m file in the Matlab editor.

I struggle with how to implement it, so I am first trying to understand how to do that with an illustration sfunction already existing in matlab : timestwo.

You can get more information by typing open('sfuntmpl.m') which is the template for sfunction.

[sys,x0,str,ts] =timestwo(0,0,1,3)

I got this error : "Error using timestwo MEX level2 S-function "timestwo" called with too many left hand arguments"

I do not understand why I have this error, and I expected to have the result of the input 1 which should be 2.

And also know if it is possible to give a vector of input and an input representing time to simulate a signal and get the result.


Solution

  • If your s-function has a .mexw32 extension then it is a compiled Level-2 C-code S-function. It is not an m-code S-function.

    I think that you'll find that there is no possible way to call either a Level-2 M-code S-function, or a Level-2 C-code S-function from anything other than a Simulink model. A level-2 m-code S-function has one input, which is a block object created and passed to it by the Simulink execution engine, while a level-2 c-code S-function is comprised of a number of functions each of which is passed a simStruct (c-code) structure, which is also created by the Simulink execution engine.

    You would have to create a model with your S-function in it, presumably along with the appropriate inport and outport blocks, then use sim to call the model.

    Note that the link you give (i.e. open('sfuntmpl.m')) is to the template for a Level-1 m-code S-function. These can be called from MATLAB as they are just a regular m-code function. If you have a Level-1 m-code S-function (which it appears you do not) then you should be able to call it in the way that you are trying to do in the question.

    The template for a level-2 m-code S-function is: edit('msfuntmpl.m');

    The template for a level-2 c-code S-function is: edit(fullfile(matlabroot,'simulink','src','sfuntmpl_basic.c'));