MATLAB: How to save the model automatically in Simulink before building it

simulink coder

I would like to automatically save my Simulink model and then generate code form it using Simulink Coder.

Best Answer

In the attached example, you can find how to implement this behavior in the OpenFcn Callback in Block Properties of a subsystem with the following script:
save_system(gcs);
rtwbuild(gcs);
If you double click the subsystem, it will save and then build the model.
You could also create a customization to the Simulink User Interface, by adding a new item to the Code Menu with a custom Build action. If you add the attached sl_customization.m to your MATLAB path and run "sl_refresh_customizations", it will add this new item to the Code menu and it can be started with Ctrl+Shift+B.