MATLAB: How to create a protected Simscape subsystem? Can I use protected models and/or S-functions

connectmodelprotectedreferenceSimscape

I have got a simple Simscape model with Connection Ports exposed out of it.
If I try to generate an S-function from it, I get the following error:
The selected system expresses connection ports. Subsystem based code generation is not supported for portions of physical models. In order to generate code, the physical model must exist entirely within a subsystem.
If I instead try to generate a protected model and create a harness from it, I get a model reference with missing connection ports.
How can I correctly create a protected Simscape subsystem/model ?

Best Answer

Model references must be standalone systems. This means that non-virtual connections, such as physical connection ports, must be fully resolved in order to be placed in a model reference.
In other words, model references and S-functions cannot be used if physical connections are exposed out of the system/model:
Some workarounds:
1) If your physical network has specified input and output parameters, such as actuation commands or sensor outputs, you may consider transforming the subsystem to have only Simulink interfaces using the PS-Simulink and Simulink-PS converters and Inport/Outport blocks. This way, the subsystem is a self-contained physical network and this will allow you to make use of protected model references.
2) You can create a custom Simscape component based on the connections of basic Simscape components by using the "connect" function in the Simscape syntax. Although this might involve substantial work to elaborate models, Simscape components created this way will preserve physical connection ports and you can use the function "ssc_protect" to obfuscate the Simscape language code comprising the Simscape component. In other words:
- write your own SSC file and programmatically connect blocks with the 'connect' function;
- protect the source code with the function 'ssc_protect';
- share the SSCP file with the third party, who can simply use a 'Simscape Component' block and link it to the protected source code.
Please refer to the following links for more information:
Starting in MATLAB R2018b, a new feature is available to automatically convert subsystems containing Simscape components into an SSC file, so that you do not need to write the source code (you will still need to protect it to preserve your IP). This can be achieved with the function subsystem2ssc .