Matlab/Simulink: Minimale leere S-Function-Vorlage
Sie können diese minimale, leere S-Function als Vorlage für den Einstieg in Ihre eigene S-Function verwenden.
Dieses minimale Beispiel enthält auch eine Vorlage für mdlStart().
Denken Sie daran, dass Sie beim Umbenennen der Datei auch das S_FUNCTION_NAME-#define entsprechend aktualisieren müssen.
nothing_sfunction.cpp
#define S_FUNCTION_NAME nothing_sfunction
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
// Function: mdlInitializeSizes ===============================================
// Abstract:
// Setup sizes of the various vectors.
static void mdlInitializeSizes(SimStruct *S)
{
// Add ssSetNumInputPorts() etc here
}
#define MDL_START
static void mdlStart(SimStruct *S)
{
// Optionally, perform initialization tasks here
// If not needed, you can remove this function and #define MDL_START entirely.
}
// Function: mdlInitializeSampleTimes =========================================
// Abstract:
// Initialize the sample times to be 1ms (1kHz)
static void mdlInitializeSampleTimes(SimStruct *S)
{
// For an example what to add here, see https://techoverflow.net/2024/10/20/matlab-level-2-s-function-example-sine-wave-continous-output/
}
// Function: mdlOutputs =======================================================
// Abstract:
// Calculate the sine wave output based on the simulation time.
static void mdlOutputs(SimStruct *S, int_T tid)
{
// For an example what to add here, see https://techoverflow.net/2024/10/20/matlab-level-2-s-function-example-sine-wave-continous-output/
}
// Function: mdlTerminate =====================================================
// Abstract:
// This function is called at the end of simulation for cleanup.
static void mdlTerminate(SimStruct *S)
{
// No cleanup required for this example
}
// Required S-function trailer
#ifdef MATLAB_MEX_FILE
#include "simulink.c" // MEX-file interface mechanism
#else
#include "cg_sfun.h" // Code generation interface
#endifKompilieren mit
build_nothing_sfunction.sh
mex -g nothing_sfunction.cpp CXXFLAGS="-std=gnu++17 -fPIC"Die Verwendung von -std=gnu++17 oder neuer wird empfohlen, ist aber nicht erforderlich.
Check out similar posts by category:
MATLAB/Simulink, C++
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow