I have included a small example which will demonstrate the workflow. This is through the usage of Legacy Code. Please see the link below for more details regarding the Legacy Code Tool.
First, include the definition of the structure by using this function: "Simulink.importExternalCTypes"
Then initialize the S function and define the Source and header files as described in the above example.
Then, specify that we intend to generate C++ code. This generates a MEX executable which can be used as an S-function block within your Simulink model. In order to prepare the data, I would suggest creating the structure data within Simulink and using this as the inputs to the S-function.
The example attached details the entire workflow. Please run the following attached script:
Dynamic memory allocation is not supported yet by Simulink in MATLAB R2020b. Therefore the direct usage of "std::vector" is not supported in Simulink or connected externally by Legacy Code Tool up to this point.
The closest workaround to mimic this behavior is by modeling the dynamically allocated memory as a pointer work vector in the context of S-Functions. Note that S-Functions generated using Legacy Code Tool also support this pointer work vector workflow. The downside of this is that the data referenced by the pointer is opaque to Simulink and therefore cannot be operated on by blocks in the Simulink library. Instead, the user has to write more S-Functions that can accept this data, re-interpret it as the correct type, and then process it.
Best Answer