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

S-function storing system time in a variable

What am I trying to do is to save in a variable (global or constant) the system time. I am using a S-function in Simulink. The problem is that when I store the value of the system time in a variable it is continuously incrementing so when I do the…
0
votes
1 answer

Model reference with C S-functions in Accelerator mode

I would like to prepare a Simulink file for a real-time target. In the main Simulink file I have a Model reference block, which I set to run in Accelerator mode. The Model block also contains several S-functions. The Model file was also configured…
remus
  • 2,635
  • 2
  • 21
  • 46
0
votes
2 answers

Some help writing a s-function to read data from serial port

After problems I had here, I need some help to write a function with MATLAB Function block. I saw in following links that some people solved it with that block or…
cyberdyne
  • 426
  • 3
  • 5
  • 23
0
votes
0 answers

Embedded Function block or S function block?

my C S-function is faster than my embedded Matlab function block in Matlab environment but when I use it in RT-Lab, the embedded is faster! does any one know why? I just used necessary routines not the optional ones.
ilegna
  • 43
  • 6
0
votes
0 answers

s-function: continuous state space

I am calling a m-file in s-function, for the continuous state space model. It is giving error in using flag with switch at line no 24. function [sys,x0,str,ts] = latitude(t,x,u,flag) % CSFUNC An example M-file S-function for defining a system of %…
0
votes
1 answer

Number of input/output ports with widths for S-function

Given a process model in a C S-function (or a .mexw file), how can one determine the number of input/output ports together with their port widths programatically in Matlab (in a .m script file) without hardcoding them?
remus
  • 2,635
  • 2
  • 21
  • 46
0
votes
1 answer

How to store arbitrary types in Simulink in block-scope?

Apparently Simulink supports only few datatypes. So, how to keep something other? I want to produces images from a directory, how to keep directory list and current position? The following code causes error function DoPostPropSetup(block) …
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
-1
votes
1 answer

Why does S-Function only execute mdlInitializeSizes() and does not execute mdlOutputs(), mdlStart() etc?

#define S_FUNCTION_NAME myfunction_sFun #define S_FUNCTION_LEVEL 2 #define MDL_INITIAL_SIZES #define MDL_INITIALIZE_SAMPLE_TIMES #include "tmwtypes.h" #include "simstruc_types.h" #include "mex.h" #include "simstruc.h" void…
Or. D
  • 1
  • 1
-1
votes
1 answer

In my code error is Index Exceeds Matrix Dimension

my code gives an error and that is Index exceeds matrix dimensions.It runs perfectly for nsample=10 but when I use nsamples=other value it gives error. The error starts from line 108 that means when dataright(1:20) = output(1:20);but it runs when…
Tamanna Islam
  • 61
  • 1
  • 1
  • 6
-2
votes
1 answer

Is there a procedure to convert m-file-function to C(or MEX) S-function?

I have a simple function written as MATLAB code. For example: function s = sum_inputs(a,b) s = a+b; This function derives from matlabFunction command application using a symbolic expression. I could like to convert it to S-Function for speed-up…
cyberdyne
  • 426
  • 3
  • 5
  • 23
-3
votes
2 answers

Why an IF statement is being infringed while output results clearly says it should be executed?

Context: I'm using Matlab/Simulink to simulate a circuit where I need to follow a reference. To simplify it: below reference current then apply more current, above reference current then apply less current. To achieve this, I have to control two…
Zrakk
  • 11
  • 2
1 2 3
8
9