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

Same S-Function used twice, one works one doesn't

I have a simulink model and I made a simple s-function block to add to my model and it works. The catch if if I copy or use that same s-function twice in my model, when I run it only the last s-function called will do what it's supposed to do, the…
0
votes
2 answers

S-function Level 2 C Simulink (R2006b) , Getting string from parameter

I don't find the way to get the string specified in the dialog box of an S-function (C, level 2) block and save it in a variable, inside the .c file corresponding to the block. Through *mxGetPr(ssGetSFcnParam(S, 0)) I can only get the first position…
Edu Fer
  • 13
  • 1
  • 1
  • 4
0
votes
1 answer

call a level-2 sfunction via the matlab editor

I have developped a simulink model. I compiled it to have a level-2 sfunction : 'sfun.mexw32'. I would like to call this sfunction in a .m file in the Matlab editor. I struggle with how to implement it, so I am first trying to understand how to do…
Chewbaka
  • 184
  • 10
0
votes
0 answers

How can I locate all occurrences of model files with the name sfunction_name?

i am using waijung blockset to programm a stmµc. And i wanted to use CAN-Bus blocks but i am facing the following error: The build process will terminate as a result. Caused by: Error, a block diagram was specified in S-function 'stm32f4_can/CAN…
Samuel
  • 53
  • 1
  • 6
0
votes
1 answer

Error using feval Undefined function or variable 'Sfun'

I have always used R, so I am quite new to Matlab and running into some troubleshooting issues. I am running some code for a tensor factorization method (available here: https://github.com/caobokai/tBNE). To start I tried to run the demo code, which…
0
votes
1 answer

How do vectors as inputs work in interpreted matlab function block in Simulink?

I have this function writen in matlab: function out = summa(in1,in2) out = in1(1)+ in1(2)+ in1(3)+ in1(4)+ in1(5)+ in1(6)+ in2(1)+ in2(2)+ in2(3) And I've implemented it in simulink as follows: And inside the matlab function block I've writen…
0
votes
0 answers

Get Sim Time in Sfunction (ssGetT)?

I used ssGetT(S), for some reason doesn't seem to work. Simple model, is the 'timestwo' with fixed time step 0.01 sec (auto solver) I call it in the mdlOutput() static void mdlOutputs(SimStruct *S, int_T tid) { int_T i; InputRealPtrsType…
Sam Gomari
  • 733
  • 4
  • 13
  • 37
0
votes
0 answers

Matlab s-function with legacy code for char* argv[]

I want to integrate a C++ function with the Matlab Legacy Code Tool. The C++ function call looks like: void init(int argc, char* argv[], struct sStruct *pStruct) What would be the corrosponding legacy code tool function specification. So far I have…
0
votes
1 answer

Reading a char array of an SFunction Parameter

I am having trouble reading a string (file name : 'aFile.csv') from SFunction parameter. The error I get doesn't make sense to me I am using MSVC++(2017). The error appears to come from ssGetSFcParam inside mxGetString Here is a snippet of the code…
Sam Gomari
  • 733
  • 4
  • 13
  • 37
0
votes
1 answer

Update S-function Parameters in Fast Restart

I have read the Mathworks documentation carefully and tried to find a solution on forums as well. However, I have not been able to find a solution to my problem yet. I am using Matlab/Simulink to simulate the dynamics of a vehicle, which picks up an…
Enrico Anderlini
  • 447
  • 6
  • 21
0
votes
1 answer

How can explain the relation between mdlDerivatives output and mdlOutputs in S-function simulink

I am new in Simulink and have a model which is used the S-function. I can not understand how the output of the mdlDerivatives relates to the outputs of the mdlOutputs? In another word, If I want to write a mfile(without using Simulink) what are the…
mvtn
  • 35
  • 8
0
votes
1 answer

Importing Matlab object in Matlab Level 2 S-function in Simulink

I was wondering if it is possible to import an object in a Matlab Level-2 S-function within Simulink. In the past, I have applied reinforcement learning to dynamic models in Matlab. As a result, I have created some classes that deal with the…
Enrico Anderlini
  • 447
  • 6
  • 21
0
votes
0 answers

fopen() in S-Function: compilation error

I try to open a file in a Simulink s-function with fopen(), but when I add the necessary lines it won't compile. Here is the error log: timestwo.c(84) : error C2275: 'int_T' : illegal use of this type as an expression C:\Program Files…
Gora
  • 61
  • 12
0
votes
0 answers

Using PWork Vector in .tlc file in MATLAB

I am trying to use PWork vectors in a custom S-Function. I am hoping to use them to store a pointer to a DLL that I need to use. In my mdlInitializeSizes I have: ssSetNumPWOrk(S,1); In mdlRTW I have: static void mdlRTW(SimStruct *S) { …
nt387
  • 53
  • 2
  • 9
0
votes
1 answer

Reaching parameters of pulse generator on MATLAB

While trying to drive steper motor from s function I have to reach the parameters of pulse generator. What I have to do is adjust the period parameter of pulse generator in order to be able to adjust the stepper motor.However, I don't know how to…
1 2 3
8 9