Matlab/Simulink S-Function: How to get full block path of S-function
In Matlab/Simulink, you can get the full block path of an S-function using ssGetPath()
.
// Retrieve the block path
const char* blockPath = ssGetPath(S);
// Log the block path
fprintf(stderr, "The full path of the block is: %s\n", blockPath);
Full example
This examle uses an S-Function without any inputs or outputs which prints the full path of the block to the command line during mdlStart()
.
#define S_FUNCTION_NAME BlockName
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
// Function: mdlInitializeSizes ==============================================
static void mdlInitializeSizes(SimStruct *S) {
ssSetNumSFcnParams(S, 0); // No parameters
if (!ssSetNumInputPorts(S, 0)) return;
if (!ssSetNumOutputPorts(S, 0)) return;
ssSetNumSampleTimes(S, 1);
ssSetOptions(S, 0);
}
#define MDL_START
static void mdlStart(SimStruct *S) {
// Retrieve the block path
const char* blockPath = ssGetPath(S);
// Log or use the block path
fprintf(stderr, "The full path of the block is: %s\n", blockPath);
}
// Function: mdlInitializeSampleTimes =========================================
static void mdlInitializeSampleTimes(SimStruct *S) {
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
// Function: mdlOutputs ======================================================
static void mdlOutputs(SimStruct *S, int_T tid) {
}
// Function: mdlTerminate ====================================================
static void mdlTerminate(SimStruct *S) {
// No termination tasks required
}
#ifdef MATLAB_MEX_FILE
#include "simulink.c" // MEX-file interface mechanism
#else
#include "cg_sfun.h" // Code generation registration function
#endif
Add this like any normal S-Function (remember is has no inputs or outputs so just place it anywhere in the model)
Example output:
The full path of the block is: MySimulinkModel/MySubsystem/TheSFunction
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow