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
4
votes
0 answers

Matlab: S-Function Builder Block: Distribute source files across multiple paths

To build to target hardware, an an S-function Builder block requires access to its source files and .mex file. It seems that the source files and the .mex file MUST be in the same directory on the Matlab path. Is it possible to place distribute the…
kando
  • 441
  • 4
  • 16
4
votes
0 answers

Matlab C++ S-Function with multiple source files

I have a simulink model with a c++ s-function. This s-function needs access to a lot of (>50) classes. Each class is consists of a header (.h) and a source (.cpp) file. I have also divided my code into multiple…
Gustav-Gans
  • 365
  • 1
  • 3
  • 13
3
votes
0 answers

Passing a string array as parameter in s-function

I'm creating a C++ mex function using the S-function builder option in simulink (matlab). I've successfully captured strings passed as a dialog box parameter in Simulink as: Simulink block dialog: Parameter : uint8('This is a…
magni
  • 31
  • 1
2
votes
1 answer

Why char is stored as 2 bytes in Matlab S-Function

I would like to pass an uint8 array to my S-Function as a parameter: inParam = char(uint8(1:7)) In the S-Function I did the followings UINT8_T *inParam = (UINT8_T *)mxGetPr(ssGetSFcnParam(S, PARAM_IN_PORT_NR)); //; but I saw that actually the…
Mokus
  • 10,174
  • 18
  • 80
  • 122
2
votes
2 answers

How to read Bus object into C struct inside C S-Function [Matlab]

I have defined a Bus object in Matlab and I am passing it to a C S-function that will do some processing. I have initialized the input like this inside mdlInitializeSizes: #if defined(MATLAB_MEX_FILE) if (ssGetSimMode(S) !=…
user1011113
  • 1,114
  • 8
  • 27
2
votes
0 answers

SFunction memory violation

I am writting a S Function that outputs a array of boolean (4 positions) I am having a access memmory violation while writing the outputs inside mdlOutputs callback. This error is not instantenous. I mean: the code runs fine until it crashes later…
guilhermecgs
  • 2,913
  • 11
  • 39
  • 69
2
votes
0 answers

how to change the s-function data using command line

can anyone tell me how to change the s-function parameters using the command line. i used the following lines to get the s-function data: blkNames = find_system(system,'lookundermasks','all','BlockType','S-Function'); blkHandle =…
Arun Kumar
  • 103
  • 13
2
votes
0 answers

Matlab/Simulink - Dymola Interface. Dymolablock out of Date

I'm trying to create a compound model of Matlab and Dymola. Therefore I followed this tutorial: Claytex tutorial Instead of the DrivenPendulum model, I made a simple model myself. I can translate the model in Dymola just fine, and the Compilation of…
EBC_Magnus
  • 41
  • 3
2
votes
1 answer

How to determine input sequence in c-mex s-function?

I am implementing a c-mex sfunction in simulink for accelerating the multiplication of two matrices. So far it is working, but I found out that the input ordering/sequence is depended of the naming schema of the connected e.g. constants. I attached…
steffenmauch
  • 353
  • 5
  • 16
2
votes
0 answers

how to get the parameters from s-function block in legacy code tool

i am working on tunable parameters and with a manual s-function its pretty straight forward. but what to do when we want to make variables tunable (getting them as parameters from s-function parameter block ) and generating the s-function…
Arun Kumar
  • 103
  • 13
2
votes
1 answer

Memory block alternative that ISN'T fixed in minor time step?

I have a model with some inputs that are fed into a CMEX S-Function via the Memory block, and the S-Function provides outputs based on these inputs, and those inputs are fed back into the S-Function. Classic algebraic loop scenario. I was using a…
user2215426
2
votes
1 answer

Pass Simulink.Parameter to C S-function

How does one pass a Simulink.Parameter structure (which, in my case, is a structure of structures) to a C S-function? Edit: Information on Simulink.Parameter You can create a Simulink.Parameter object this way: modelParameters =…
remus
  • 2,635
  • 2
  • 21
  • 46
2
votes
1 answer

Dynamically sized bus objects in Simulink

I wrote a C S function which has a variable number of states depending on one parameter, which is passed to it (I'm using computational fluid dynamics and the parameter is the number of cells). I want to output a bus object from my S function that…
remus
  • 2,635
  • 2
  • 21
  • 46
1
vote
1 answer

S-Function uint64_T input/output for simulink code generation error

I am attempting to generate C code with simulink coder for a model with an S-Function on Matlab 2020b. The S-Function contains an int64 input and an int64 output. The update diagram and simulink simulation work fine, therefore my s-function is…
1
vote
0 answers

Simulink Legacy Code Tool - custom Arduino servo write block problem

I'm trying to create my own servo.write block in Simulink for Arduino DUE deployment (and External Mode). Before you ask why if there is one available inside the Simulink Arduino Support Package, generally my final goal is to create a block that…
1
2 3
8 9