MATLAB: Automate SFunction generation – Simulink workflow

code generationsimulink

Hi,
I've got a subsystem block that I'm generating an SFunction of, which I'm then using in a model.
How do I automate the generation of the SFunction so that when the subsystem changes the SFunction is regenerated, so I can be sure I have the latest version? Any script I can write to do this will be called when the library file containing the subsystem changes, but I don't know what to put in the script.
As far as I can see, if the SFunction is regenerated it will replace all the code files – so it isn't necessary to make any changes to the model that uses it. Please correct me if I'm wrong in this assumption. (This is assuming I can treat the block as a black box and the inputs/outputs won't be changing).
Many thanks,

Best Answer

Try the following:
set_param('subsystem_model', 'SystemTargetFile', 'rtwsfcn.tlc');
set_param('subsystem_model', 'CreateModel', 'off');
rtwbuild('subsystem_model/subsystem_name');