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

How to use Global Objects in Matlab S-Function

I need to use an object among several functions on a Matlab S-Function. The object is used as a library and it is needed to set the conection with a server, get data from it in each loop and close it at the end of simulation. The use of the object…
DdS
  • 11
  • 3
1
vote
1 answer

Using the ssSetTNext S-function macro outside of mdlGetTimeOfNextVarHit method

The Simulink documentation for ssSetTNext says that: "A discrete S-function with a variable sample time should use this macro in mdlGetTimeOfNextVarHit to specify the time of the next sample hit". Now, I'd like to compute the next sample hit for the…
Matteo M.
  • 65
  • 1
  • 3
1
vote
1 answer

Multi-Instance usage of S-Functions (C code) in SIMULINK

I have a legacy C function in my SIMULINK model that uses some static variables in its functions. The static variables are available globally. When I reference multiple instance of my model, I get error because my S-function is not configured for…
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
1
vote
2 answers

how to create a custom s-function in simulink?

What is the block that I can use for the creating a custom s-function in simulink? I want to use C code using S-Function.. I know that there are functions that I have to define but I don't know how to reach it or which code to start with exactly..…
user1413188
  • 91
  • 5
  • 14
0
votes
0 answers

Make a Simulink block with dynamiclly changing value

My overall goal is to use a moving average filter where the filter length changes dynamiclly. My problem is that I really do not know how to change the parametrs in a simulink block. It seems like they are ment to be constant. I am working with a…
0
votes
1 answer

Change the default compiler from lcc to MSVS running in Rapid accelerator mode

I am trying to run a Simulink file which contain S-functions along with other Simulink blocks in rapid accelerator mode. When tried to build the executable I am getting a error to change the compiler from LCC to supported compilers. Note: Matlab…
Akhil Nandan
  • 315
  • 2
  • 9
0
votes
0 answers

How to run multiple S-function modules in one MATLAB/Simulink model?

The flag of the S-function module is a variable defined in the system. The flag of one S-function affects the flag of another S-function, resulting in a running error. I would like to ask if there is any way to run multiple S-function modules in one…
LEON
  • 1
  • 1
0
votes
1 answer

Is there a way to integrate STM32 Arduino code into Simulink?

My goal I'm building a Simulink based application for Nucleo L476RG using Mathworks' code generation support packages. These packages include basic blocks to control the peripherals of the controller but when it comes to external hardware (f.e. an…
C. Matr
  • 3
  • 2
0
votes
0 answers

MATLAB s-function 2 level, how to rename parameters

[enter image description here][1] Hi. I created matlab level-2 s-function where i want a user to put two parameters. How am i supposed to add second field for a second parameter (and not just type them separated with comma)? And also how am i…
ureadulose
  • 15
  • 2
0
votes
0 answers

Enabling Variable Time Step in Level-2 Fortran S-function

I'm trying to make my level-2 Fortran S-function have a variable step time so that the output variables when ran in Simulink are continuous (right now the outputs are very choppy and step-function like). I can't use a fixed time step solver due to…
Erin
  • 1
0
votes
0 answers

Calling a function-call subsystem from C s-function with inputs and outputs

I am trying to call a Simulink function-call subsystem from within an s-function written in C. This is in principle possible as documented in this tutorial: Implement Function-Call Subsystems with S-Functions. What this example does is something…
SampleTime
  • 291
  • 3
  • 19
0
votes
0 answers

Logging S-function internal variables to workspace

I have a simulation environment in Simulink, in which I have some ECU code (written in C), wrapped in an S-function, that is connected to a vehicle plant model. In this C code, I have global variables that are there for logging purposes. How do I…
Ben
  • 1
0
votes
0 answers

Transforming a transfer function into a differential equation in Matlab

I have the following code in matlab: syms s num = [2.4e8]; den = [1 72 90^2]; hs = poly2sym(num, s)/poly2sym(den, s); hs f = ilaplace(hs) The inverse Laplace transform converts the transfer function in the "s" domain to the time domain.I want to…
user13053456
  • 35
  • 1
  • 4
0
votes
2 answers

I need to write a simulink s-function that is triggered by an input signal

I wish to write data to a file when an integrator saturates. I would like to route the integrator's saturation output port to one of the s-function's inputs. I realize that I can continuously poll the signal, but I'd like to explore triggering on…
0
votes
1 answer

Manage HTTP response (uint_8 decimal vector) and use http parser to read JSON values

I am working in Simulink on a TCP connection between client (my computer) and server. Through standard Ethernet blocks, I send an HTTP request like the following: GET /status HTTP/1.1 Host:... Accept: application/json To send it, I convert it with…
nick_bl
  • 23
  • 2
1 2 3
8 9