call a level-2 sfunction via the matlab editor - matlab

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.

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'));

Related

Simulink code generation skips my matlab function

I have a matlab function in Simulink which has one input and no output. I get some data from inport into this function and dump those variables into global variables that already exists in the workspace. However, Simulink code generation completely skips this matlab function and I'm not sure how to force it to go through those assignments.
Goal: I'm trying to update some workspace variables that are used throughout the model using inports.

Use logged signal in Matlab Level-2 S-function (in Simulink)

Is it possible to use logged signals within Matlab Level-2 S-functions?
I need an S-function to run only every 5 s, so that that could be its rate. However, if I use the S-function to log the data, I need to call it at every time step and that slows down the code considerably. Is it possible to use logged signals within the Matlab Level-2 S-function instead?
Thank you in advance for the help!

What is the use of S-function in simulink?

I have created a simulink model and I need to convert it to C/C++ code using S-function. Can anyone tell me how to create S-function and where and how to use this to generate code?
S-functions (system-functions) provide a powerful mechanism for extending the capabilities of the Simulink environment. An S-function is a computer language description of a Simulink block written in MATLAB, C, C++, or Fortran. C, C++, and Fortran S-functions are compiled as MEX files using the mex utility
Instead of using Simulink Blocks which I Suppose you are telling to create a simulink model, you can use s-function builder to write your code and go for Matlab Coder for converting it to Embedded C-Code if you want to build your code on any Microcontroller

RMS not supported in Matlab function inside Simulink

Simulink has a module called "Matlab Function," which allows you to create a custom function in a Simulink flow diagram.
I implemented a simple function in a Simulink Matlab Function module. My function contains a call to Matlab's built-in rms(). When I run the Simulink model, I get the following error:
The function 'rms' not supported for standalone code generation
If I remove rms from my Matlab Function in the Simulink model, the error goes away and the model runs flawlessly.
Questions:
Is there a way to use Matlab's rms in Simulink?
Are there many other native Matlab calls that can't be used inside Simulink?
I just wanted to clarify and expand upon some points made in learnvst's answer.
Even if you are simply trying to simulate a model containing a MATLAB Function block and are not explicitly attempting to perform code generation, you will still get the not supported for standalone code generation error.
As learnvst indicated, there are multiple restrictions on functions that can be used with code generation. However, if you just want to simulate your model, Simulink allows you to do this if you signify these "black-listed" functions as being extrinsic. This lets Simulink know that the functions will be used for simulation purposes only and won't be part of code generation.
In your particular case, add the following line of code somewhere before your call to rms:
coder.extrinsic('rms');
Declaring a function as extrinsic in a MATLAB Function is often useful even when you are performing code generation. For example, you may want to visualize your data using the plot command during simulation, but obviously would not need the plot command to be part of generated code.
Refer to this doc for more info on declaring functions to be extrinsic.
The not supported for standalone code generation part of the error suggests to me that you are trying to use a product like Matlab Coder to make an executable or native code. If this is the case, there are many naive calls that cannot be used directly in both core Matlab and the toolboxes. The coder products only support a subset of the language. More information can be found here . . .
http://www.mathworks.co.uk/products/matlab-coder/description2.html
As for your call to rms, it is only calculating the root of the mean of the squares. Try creating an alternative using something like . . .
sqrt(mean(x.^2))
...where x is the signal.

How can I call an m file in Simulink and put it to a block in my model?

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.
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.
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