| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A comprehensive, production-ready template for creating custom S-functions that interface C++ code with Simulink. This enables seamless integration of third-party libraries and custom algorithms into Simulink models and supports automated code generation with Simulink Coder.
S-functions (System-functions) provide a powerful mechanism to extend Simulink with custom C++ implementations. This repository demonstrates best practices for:
├── CMakeLists.txt # Build configuration with FindMatlab.cmake ├── cmake/ │ └── FindMatlab.cmake # MATLAB auto-detection module ├── include/ │ └── optical_flow_velocity.hpp # Custom class headers (Doxygen documented) ├── src/ │ ├── s_function.cpp # S-function implementation (fully documented) │ └── optical_flow_velocity.cpp # Custom class implementation ├── test_s_function.slx # Example Simulink model ├── include_directories.txt # Auto-generated (include paths) ├── link_directories.txt # Auto-generated (library search paths) ├── link_libraries.txt # Auto-generated (library files with extensions) ├── source_files.txt # Auto-generated (source files) ├── LICENSE └── README.md
# Clone the repository
git clone https://github.com/amaldevh/s-function.git
cd s-function
# Create build directory
mkdir build && cd build
# Configure and build (MATLAB auto-detected via FindMatlab.cmake)
cmake ..The build process will:
Or specify MATLAB location explicitly:
On Windows:
cmake .. -DMATLAB_ROOT="C:/Program Files/MATLAB/R2023b"On Linux/macOS:
cmake .. -DMATLAB_ROOT=/usr/local/MATLAB/R2023bSee test_s_function.slx for a complete example.
Install OpenCV, for example the precompiled binaries from sourceforge at OpenCV Compiled library Sourceforge
Install the MinGW-64 C/C++ toolbox from Matlab file exchange at MingW-64
Find Mingw-64 path by executing following command in Matlab window
%% Find the value of MW_MINGW64_LOC set by matlab
mingw_path = getenv('MW_MINGW64_LOC')Open Powershell and execute
# Extract the OpenCV installation
# Set the MW_MINGW64_LOC env var
$env:MW_MINGW64_LOC = "mingw_path"
# Clone the repository
git clone https://github.com/amaldevh/s-function.git
cd s-function
# Create build directory
mkdir build && cd build
# Configure and build (MATLAB auto-detected via FindMatlab.cmake)
cmake .. -DCMAKE_PREFIX_PATH="\path\to\opencv\build\dir"Place your C++ source files in src/ and headers in include/:
// include/your_algorithm.hpp
/**
* @brief Your custom algorithm class
*/
class YourAlgorithm {
public:
YourAlgorithm(double param1, double param2);
void processData(const std::vector<double>& input);
std::vector<double> getResults() const;
};Choose one of two memory management strategies in s_function.cpp:
Option A: Persistent Memory (Recommended)
#define USE_PERSISTENT_MEMORYOption B: Static Objects
// Comment out or remove USE_PERSISTENT_MEMORY definition
// #define USE_PERSISTENT_MEMORYAll callbacks are documented in src/s_function.cpp. Key functions:
Configure ports and parameters:
ssSetNumSFcnParams(S, num_parameters);
ssSetNumInputPorts(S, num_inputs);
ssSetNumOutputPorts(S, num_outputs);Initialize your objects:
YourAlgorithm* algo = new YourAlgorithm(param1, param2);
ssSetPWorkValue(S, 0, static_cast<void*>(algo));Process data each timestep:
YourAlgorithm* algo = static_cast<YourAlgorithm*>(ssGetPWorkValue(S, 0));
InputRealPtrsType inputs = ssGetInputPortRealSignalPtrs(S, 0);
real_T* outputs = ssGetOutputPortRealSignal(S, 0);
// Process inputs and write to outputsClean up resources:
YourAlgorithm* algo = static_cast<YourAlgorithm*>(ssGetPWorkValue(S, 0));
delete algo;Edit CMakeLists.txt to add your libraries:
set(CUSTOM_PACKAGES
OpenCV
Eigen3
YourLibrary
)
set(SRCS
${CMAKE_SOURCE_DIR}/src/s_function.cpp
${CMAKE_SOURCE_DIR}/src/your_implementation.cpp
)cd build
cmake .. # FindMatlab.cmake will auto-detect MATLAB
# Or specify MATLAB location:
# cmake .. -DMATLAB_ROOT=/path/to/matlab
# View build output
# Generated files:
# - matlab_simulink_s_function.mex* (your S-function)
# - include_directories.txt (include paths)
# - link_directories.txt (library search paths)
# - link_libraries.txt (full library paths with extensions)
# - source_files.txt (source files)For Simulink Coder code generation:
Note: The CMake build automatically generates these files with:
The included example demonstrates real-time optical flow tracking for UAV applications:
Features:
Inputs:
Outputs:
Algorithm:
The CMake build process automatically generates 4 text files for Simulink Coder integration:
Contains full paths to include directories for header files.
Example content:
/usr/include/opencv4 /home/user/project/include
Simulink field: Code Generation → Custom Code → Include directories
Contains library search paths where linker should look for libraries.
Example content:
/usr/lib/x86_64-linux-gnu /usr/local/lib
Simulink field: Code Generation → Custom Code → Library search path
Contains full library file paths with platform-specific extensions.
Example content (Linux):
/usr/lib/x86_64-linux-gnu/libopencv_core.so /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so /usr/lib/x86_64-linux-gnu/libopencv_video.so
Example content (Windows):
C:/opencv/lib/opencv_core454.lib C:/opencv/lib/opencv_imgproc454.lib
Simulink field: Code Generation → Custom Code → Libraries
Contains paths to all source files that need to be compiled.
Example content:
/home/user/project/src/s_function.cpp /home/user/project/src/optical_flow_velocity.cpp
Simulink field: Code Generation → Custom Code → Source files
This template supports two memory management strategies:
| Feature | Persistent Memory | Static Objects |
|---|---|---|
| Complexity | More explicit | Simpler |
| Cleanup | Manual (mdlTerminate) | Automatic |
| Multi-instance | Requires unique IDs | Native support via map |
| Use case | Single complex object | Multiple lightweight instances |
Problem: MATLAB_BIN_DIR not set
Solution: Specify MATLAB path explicitly: cmake .. -DMATLAB_BIN_DIR=/path/to/matlab/bin
Problem: Could not find OpenCV
Solution: Install OpenCV or specify OpenCV_DIR: cmake .. -DOpenCV_DIR=/path/to/opencv/build
Problem: Compiler not compatible with MATLAB
Solution: Check MATLAB documentation for supported compilers: mex -setup C++
Problem: Optical flow tracker not initialized
Solution: Ensure mdlStart() completes successfully. Check MATLAB console for errors.
Problem: Incorrect output dimensions
Solution: Verify output port dimensions match your algorithm's output size.
Problem: Memory leaks in simulation
Solution: Ensure mdlTerminate() properly deletes all allocated objects.
Problem: S-function not found in Simulink
Solution:
1. Copy MEX file to model directory
2. Add build directory to MATLAB path: addpath('build')
Problem: Parameter mismatch error
Solution: Verify number of S-function parameters matches ssSetNumSFcnParams() call.
If you find this repository helpful, please ⭐ star it!
Contributions are welcome! Please:
See the LICENSE file for details.
| Back | FazBrowse Home | New Git URL |