Questions tagged [s-function]

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. By following a set of simple rules, you can implement an algorithm in an S-function and use the S-Function block to add it to a Simulink model.

Taken from MathWorks:

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 (see Build MEX-File). As with other MEX files, S-functions are dynamically linked subroutines that the MATLAB interpreter can automatically load and execute.

S-functions use a special calling syntax called the S-function API that enables you to interact with the Simulink engine. This interaction is very similar to the interaction that takes place between the engine and built-in Simulink blocks.

S-functions follow a general form and can accommodate continuous, discrete, and hybrid systems. By following a set of simple rules, you can implement an algorithm in an S-function and use the S-Function block to add it to a Simulink model. After you write your S-function and place its name in an S-Function block (available in the User-Defined Functions block library), you can customize the user interface using masking (see What Are Masks?).

If you have Simulink Coder™, you can use S-functions with the software. You can also customize the code generated for S-functions by writing a Target Language Compiler (TLC) file. For more information, see Insert S-Function Code.

131 questions
1
vote
1 answer

Simulink: how to store a local variable in level 2 S-function?

I'm starting from msfuntmpl_basic.m and modifying it to output audio from an audio device. Where can I store the device (dsp.AudioRecorder)?
Chuck Carlson
  • 977
  • 8
  • 26
1
vote
0 answers

ssSetOutputPortWidth - does it allocate memory? Can we force that it is contiguous?

I am writing a C S-function in Simulink/Matlab, which fills an output buffer of size 500 kB for a later application. I have done this in mdlInitializeSizes: ssSetOutputPortWidth(S, 0, 500000); ssSetOutputPortDataType(S, 0, SS_UINT8); Later, in…
user1011113
  • 1,114
  • 8
  • 27
1
vote
1 answer

Mathworks Simulink: S-Function Builder

I have a library with numerous S-function Builder blocks. If I want to regenerate .mex files (say, on a different platform), the standard method would be to: Go to every S-function builder block Open its S-Function Builder Wizard Click…
kando
  • 441
  • 4
  • 16
1
vote
2 answers

Error using malloc inside SFunction

I´m developing a C-MEX SFunction to integrate with simulink. The objective here is to convert an array of ascii codes do double. Before opening matlab, I implemented a test code using Visual Studio and it works just fine. (see below) #include…
guilhermecgs
  • 2,913
  • 11
  • 39
  • 69
1
vote
0 answers

Can a mex s-function component be recompiled only if it changed?

In the first phase of testing my code has to run in a Matlab Simulink environment (2010b 32bits) compiled as S-function. The compiler is the one that comes with Visual Studio 2010. I currently have a script that compiles my code that looks like…
Federico
  • 1,092
  • 3
  • 16
  • 36
1
vote
1 answer

Add external C library on Embbeded coder Simulink (for Xilinx "Zedboard" target Xilinx SDK) in S-Builder

I'm currently working on a project to be implemented on a Xilinx Zedboard, using Simulink Embedded Coder methodology. Now I have to build a block that connects to a database (via MySQL, using the C library). The problem is that I don't know how to…
kiraito
  • 13
  • 3
1
vote
1 answer

s-function as a bus selector

i need an idea how to make an s-function behave as a bus selector. i have an structure being given as a input to an s-function . this structure has 283 elements (maybe more in future) and is being supplied as a bus. i want the s-function to output…
Arun Kumar
  • 103
  • 13
1
vote
1 answer

Specify port types in s-function blocks

I'm building my first s-function block from compiled C code. Everything is going fine, except that the s-function block demands that its interface variables are of type double, even though the underlying C interface variables are not. The block…
Emilio M Bumachar
  • 2,532
  • 3
  • 26
  • 30
1
vote
1 answer

how to make constant variables being used in s-functions as tunable in matlab

i have written a simple s-function which calls a function inside a c code which models the single track model. i am using constant variables for storing mass,yaw moment of inertia,steering ratio etc. I wanted to know how to make these variables…
Arun Kumar
  • 103
  • 13
1
vote
0 answers

Why should I use SFUNPRINTF?

I was going through a .cpp file which is the source for a MATLAB S-Function and I noticed that to print on the "console" the programmer wrote: SFUNPRINTF("Text I want to print on the console"); While I would have done: printf("Text I want to print…
desmond13
  • 2,913
  • 3
  • 29
  • 46
1
vote
1 answer

STRINGIFY in .cpp file

I am reading some .cpp files made to compile them and have a mex file to use in MATLAB for a Level-2 S-Function. I would like to know what is this instruction IN PRACTICE for: #define STRINGIFY(x) #x #define TOSTRING(x) STRINGIFY(x) Thanks for…
desmond13
  • 2,913
  • 3
  • 29
  • 46
1
vote
0 answers

How to compile a bus with a 'large' number of signals?

I have a simulink model with a c++ SFunction I would like to send a large nested bus into, however the compiler crashes after about ten minutes with a lack of heap space error. The bus has nine buses inside it and a further set of buses inside…
J Collins
  • 2,106
  • 1
  • 23
  • 30
1
vote
1 answer

Conditionally enabled rtwoptions in a custom System Target File (Simulink Coder)

I'm creating a custom System Target File (STF) and adding some RTW options that need to be configured. Some options are dependent on others, and may or may not make sense depending on how they are configured. In these cases I would like to…
pmb
  • 866
  • 7
  • 24
1
vote
0 answers

How to distribute (arrange) blocks programatically in a Simulink library?

I'm using Simulink's Legacy Code Tool to generate an S-Function block driver library from legacy C device drivers for an embedded target. Driver blocks are generated iteratively and end up in the library target_LCT_sfun_drivers by way of LCT's…
earGrowth
  • 761
  • 5
  • 10
1
vote
1 answer

Matlab Simulink: How to specify a definite solver step size for every iteration?

I want to set a variable step size for every solver step by using the command in the S-function like: dT= ... % calculate the dT from the inputs of Block and the parameters of S-function set_param(gcs,'...',num2str(dT)); However, the Matlab does…
user3518207
  • 21
  • 1
  • 3
1 2
3
8 9