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
0
votes
1 answer

Simulink with if/else and different signal dimension

I need to implement a if/else in simulink to find out if a input is a scalar value or a matrix. Please see, the diagram below : Given: Block(1) - is a input that can be a scalar "1" or a matrix "[[0 15];[5 10]]" Block(2) - must return the signal…
guilhermecgs
  • 2,913
  • 11
  • 39
  • 69
0
votes
1 answer

creating .txt file from S function

I have an C mex S function which can print output to the .txt file. This code inside mdlStart(SimStruct *S) creates datafile.txt file and appends the output. void** pwork = ssGetPWork(S); FILE *datafile; datafile =…
redumpt
  • 167
  • 13
0
votes
1 answer

Call mex Function from cmex SFunction

I have implemented a complex mexFunction using visual studio 2012 and successfully integrated it with Matlab. (lets call it mexFunctionA.mexw32 ) When I run this command in matlab command window, I get the expected…
guilhermecgs
  • 2,913
  • 11
  • 39
  • 69
0
votes
1 answer

Click automatically in specific points after recognition of text

I am using MATLAB and SIMULINK and there is something missing there that is driving me crazy, but the question is not MATLAB-related. At a certain point I have a window like that: I would like to have the SELECT ALL button. I don't know why, but…
desmond13
  • 2,913
  • 3
  • 29
  • 46
0
votes
1 answer

Level1 Matlab S-function - "Work Vectors"

I'm using a Matlab level 1 S-function several times in a model but don't want the mutual overwriting of global/persistent variables. A solution could be work vectors but there is little documentation about level 1 S-function work vectors. Where do i…
0
votes
1 answer

S-function not found by Simulink unless rebuild

I am currently using Matlab Simulink with a Raspberry Pi. I have been using the S-Function Builder block to generate an S-Function that I am using with the S-Function Block. However, I always need to keep a copy of the S-Function builder that I need…
Domack
  • 75
  • 11
0
votes
0 answers

level 2 c-mex function input return wrong value

I have a TLC generated mexfunction with 2 inputs and 0 output. input 1 is unsigned char and input 2 is unsigned int. When I try to get input values with: unsigned char *u1 = ssGetInputPortSignal(S,0); (uint8) unsigned int *u2 =…
0
votes
1 answer

How to use the variables of a simulink model in matlab m file?

I hope my keywords describe my situation. Basically, I'm working with SMC (Sliding Mode Control) in Simulink using simulink blocks and S Function blocks. Now, I want to use DE (Differential Evolution) algorithm, to determine the parameters of SMC…
AlFagera
  • 91
  • 1
  • 10
0
votes
0 answers

Use Run-time Parameters in a Matlab-code S-Function

I have to write a Level-2 S-Function in Matlab code (C is not viable) which takes in some parameters and uses them alone and in combination. First I tried tunable parameters, but unfortunately I haven't succeded in tuning them (the documentation…
Astrinus
  • 412
  • 9
  • 19
0
votes
1 answer

How to pass a Matrix in Simulink using S-Function

I have written a S-Function in MATLAB. Now I want to pass a Matrix into the input via the 'simin' Source. I have initialized the time of the simin variable with simin.time = 0 and the values are stored in a dynamically sized matrix. So for example…
Tester123
  • 15
  • 5
0
votes
1 answer

Setting a Level-2 Matlab S-Function Block Sample-Time to specific time intervals

Matlab beginner here and I'm stuck on some problem and neither Matlab documentation nor other posts here could help me. I'm working on a Level-2 Matlab S-Function that receives a sensor signal via USB (Gyroscope, Magnetometer and Accelerator…
Unfixable
  • 440
  • 1
  • 7
  • 20
0
votes
1 answer

Matlab crash when running user defined blocks in simulink

I created a user defined block with single input and single output in simulink and it works perfectly fine. But when i create a user defined block without an input, run this block in a simulink model, MATLAB Crashes. Below is the code for the C…
Be_bro
  • 15
  • 1
  • 5
0
votes
1 answer

Create customized S function blocks and also generate C code using real time workshop for the same

I want to create customized S function blocks and also generate C code for them using real time workshop. I could not quite find the correct documents for this task. Could someone please help me by pointing to correct and easy documentation?
Be_bro
  • 15
  • 1
  • 5
0
votes
1 answer

C S-function to read data from serial

After issues I met, trying to read from COM port with Simulink, as I wrote in this discussion, I would like to follow Phil Goddard's suggestion on writing a C S-function for that. My problem is that I am not able to write C-code, so I hope in some…
cyberdyne
  • 426
  • 3
  • 5
  • 23
0
votes
1 answer

how to input Matlab "Bus Objects" in to "Level-2 MATLAB S-Functions"

is it possible to use bus datatypes for m-file s-function blocks in Simulink? I haven't found any examples for this in the matlab documentation or anywhere online. For s-Functions written in C the API looks somewhat like this DTypeId…
rava
  • 183
  • 12
1 2 3
8 9