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

I need to install gcc4.9 on ubuntu 20.04 | matlab mex

On matlab, I was trying to run mex file.c but I got a warning and error message. Anyway, I think the problem is what the warning says: Warning: You are using gcc version '9.3.0'. The version of gcc is not supported. The version currently supported…
acsarda
  • 11
  • 1
  • 2
1
vote
0 answers

Configuring the output of an S-Function

I have the following codes in Matlab. function dx=massamola(t,x,u) m=1; k=1; b=1; x1 = x(1); x2 = x(2); dx1 = x(2); dx2 = -(k/m)*x1-(b/m)*x2+(1/m)*u; dx=[dx1;dx2]; soon after I have a specific code of an S-Function. function…
user13053456
  • 35
  • 1
  • 4
1
vote
0 answers

Remove HTTP headers and parse JSON payload in C with tiny-json (no libcurl)

As above, I'm trying to manage HTTP response with JSON payload without any library like curl or others. Everything is written inside an S-function in Simulink environment but the "main" is (hoping) perfectly C. My procedure: take the uint8 array…
nick_bl
  • 23
  • 2
1
vote
1 answer

C file not instrumented for coverage, bad expr node kind

I am using the legacy_code tool in MATLAB, to generate some S Functions, then I want the S Functions to be under analysis by the simulink coverage toolbox. I am asking also here because maybe this is a C issue and not MATLAB related. I am setting to…
Luis
  • 127
  • 1
  • 11
1
vote
1 answer

How do I flip a timeseries data in Simulink?

I have been trying to multiply 2 sets of timeseries data in Simulink, At and Bt, and I expected the result to be like this: ans = sum(A(1:t)*B(t:-1:1)) For instance, when t = 3, the result should be ans =At1*Bt1 + (At2*Bt1 + At1*Bt2) + (At3*Bt1 +…
CJW
  • 11
  • 2
1
vote
0 answers

MATLAB/Simulink - C MEX S-function: Transformation between bus signal and legacy C structure involving pointers

currently I am implementing legacy C Code into C MEX S-functions at MATLAB/Simulink (R2017b). I have already put a duplicate entry on Entry at MathWorks-MATLAB Answers. The legacy functions take as a general rule pointer to structures involving…
1
vote
2 answers

Share data between multiple c mex s-functions

I am implementing several c s-functions. They have to rely on the same pointers and variables, inedependet of the current s-function. Basically I want to instantiate all variabels and pointers in one "setup" s-function (inside mdlInitialize) and…
Lemonbonbon
  • 686
  • 5
  • 25
1
vote
0 answers

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…
Enrico Anderlini
  • 447
  • 6
  • 21
1
vote
0 answers

Matlab S-function Error

I'm trying to build a tracker differentiator using Matlab S-Function. However, it's giving me an error saying "Output returned by S-function 'han_td' in 'ex_han/S-Function' during flag=3 call must be a real vector of length 2". I can't see what I'm…
D.Bombu
  • 11
  • 1
1
vote
0 answers

How to use OpenCV in a C++ S-Function

I want to use the OpenCV function cvtColor to change and image from RGB to Grayscale using a S-Function in Simulink, like this: In order for the input to be used with OpenCV it needs to be converted into cv::Mat. I found that this can be done with…
Onion
  • 11
  • 4
1
vote
1 answer

Calling fmincon from Simulink

I am trying to implement a particular type of model predictive control in the Simulink-Matlab framework. To do so, my plan was to have the dynamic model in Simulink call an external Matlab S-function which in turns runs an optimization that calls a…
Enrico Anderlini
  • 447
  • 6
  • 21
1
vote
0 answers

MATLAB bus from struct

I want to hand data from a struct in my MATLAB workspace to an S-function. To do this I created a bus. The code is compiled without any errors. When I start the Simulation I get the following error message: Invalid structure-format variable…
J.M.
  • 25
  • 9
1
vote
0 answers

"mexFunction is missing" Error while obtaining sizes from MEX S-Function in Matlab

First of all thanks a lot for reading this post and being able to help. I have an .cpp custom code and I could create a mexa64 typing: mex mycpp.cpp Now I would like to implement this .mexa64 file into Simulink. I have create a mycpp.c and…
Smith
  • 33
  • 7
1
vote
1 answer

global variable not initialized in level 2 s-function

I am trying to make a level 2 s-function which would act like a buffer (I just started learning S-functions). Now what I want is that every time an input comes in it gets stored in the next index until the buffer becomes full then it starts to push…
Novice_Developer
  • 1,432
  • 2
  • 19
  • 33
1
vote
0 answers

Simulink: Setting the output of a continuous S-Function

I have a continuous S-Function that solves the derivatives for various state properties within a ICE cylinder. As such, the output of the function is set to output the integral of those derivatives for each time step which is a 7 element vector (1…
Chris Lim
  • 21
  • 2
1
2
3
8 9