What is the use of S-function in simulink? - matlab

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

Related

Matlab Simulink and Reinforcement Learning Approach

I would like to create a project using a Matlab simulink environment model to find optimal parameters using reinforcement learning. Is it possible to export the environment data from matlab simulink and incorporate it into a python script or to create an interface to matlab simulink in order to train interactively with it? Or is there a better approach?
Unfortunately, this is rather vague, so I'm sorry, as I do not know what the matlab model will look like. It would also be helpful for me to see an example project or something along the lines of accessing Matlab Simulink data and reinforcement learning.
Thank you very much.
To create an interface with python script and the Simulink simulation, you can use MATLAB Engine for Python.
I could find a sample implementation in this blog. You can extend the simulate function in this example with an RL Agent to determine an action.
You can use simulink and generate coder from simulink. You must download simulink coder, matlab coder packages to generate code.

Using Application Compiler for app including rtwbuild()

I would like to use the Application Compiler (Appdesigner -->Standalone Desktop App) on my MATLAB app. This app calls a .m file which is using the rtwbuild command to deploy a Simulink model to an arduino. The app works well when run in MATLAB, however after compilation I receive an error with rtwbuild:
Error using rtwbuild
The Matlab Path must have these directories for Simulink to function:
toolbox/simulink/blocks, toolbox/simulink/simulink
Matlab cannot continue. Please restore the path and restart Matlab
The documentation says, that no Simulink functions are supported for the MATLAB Compiler (see here) and the rtwbuild function seems to be a Simulink function, since it is in the documentation in the category "Simulink Coder" (see here). However, I do not have the Simulink Coder Toolbox and the app was running in MATLAB anyway, so I am not sure if rtwbuild is really a Simulink function.
To keep it short, my question is the following:
"Is it possible to use the Application Compiler on .m scripts including the rtwbuild command?"
Thank you for your help!
rtwbuild is without a doubt a Simulink Function, which means the MATLAB-Coder can not generate code. More generally speaking, you can not deploy the code generator. Generating code from M or Simulink requires a full MATLAB (Simulink) to be running. It would be foolish to let you use such a key feature without a MATLAB (Simulink) license (personal opinion).
About your confusion about rtwbild being part of the Simulink Coder. The Support Package for Arduino Hardware allows you to use certain deployment features. You can for example get binaries, you can not access the C code without having a Coder License. For details refer to "Additional Capabilities with Simulink Coder and Embedded Coder" in the documentation.
The documentation does not reflect these special cases, but obviously TMW did not re-invent the wheel when providing the Support-Packages for Arduino. Instead some "Simulink Coder" functions do not require a Simulink Coder License when used with the code generation Target Arduino. Same is true for the Raspberry and to a limited extend for the (rapid) accelerator mode.

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

Generating c++ Code from *.slx

I have a question regarding generating C++ code from a Simulink diagrams (*.slx file). I know that one could generate c/c++ and header files using Simulink Coder toolbox. Is there any other way to generate c/c++ file from a Simulink diagrams? Maybe Embedded Coder?(I have tested codegen, but does not work, since it generate c++ code from an M-file)
You have to get a license for the Simulink Coder. You may take a look at the alternatives I mentioned in my comment, but as long as the Simulink Coder satisfies your requirements, it's the cheapest option.

convert matlab code to c code

is there s a way to convert simulink blocks or a matlab ".m" code to C code automatically ?
I'm not aware of any direct translation tool, but there are a few alternatives:
Matlab Compiler will let you create a shared library (callable from C code) from your .m code.
GNU Octave is an open source interpreter that has many of the same functions as Matlab. It is open source, and written in C. You might feasibly build a C library based on on this, although it would be a lot more heavy-weight than option (1).
To convert Simulink models or MATLAB m-code to C you need Real-Time Workshop.
It supports only subset of MATLAB language and oriented for embedded systems.
Look also at other MatWorks products for code generation and application deployment:
http://www.mathworks.com/products
With MATLAB Compiler you will not get a C code, but a binary code, executable or library (dll), which will run on machines without MATLAB installed, but with MATLAB Compiler Runtime (MCR) library. It quite large in size, and platform specific and I believe it has to match the MATLAB version of compiled code.
For the new coming release 2011a The MathWorks developed new code generation products: MATLAB Coder, Simulink Coder and Embedded Coder.