As you have correctly mentioned, you can use Simulink's 'PreSaveFcn' callback to prevent saving a model after changes. The 'PreSaveFcn' executes before the model is saved, so you can use it to chcek if modifications have been made and then prevent saving.
You can follow the below given steps to implement:
- Open your Simulink model.
- Go to Model properties > Callback > PreSaveFcn.
- Add the following MATLAB code in the 'PreSaveFcn' callback:
modelFile=which(modelName);
if strcmp(get_param(modelName,'Dirty'),'on')
errordlg('Saving is disabled for this model after changes.Revert changes to save.','Save Disabled');
error('Saving is disabled for this model after modification.');
This will throw an error saying "Saving is disabled for this model after modification.".
To prevent saving model when the version changes, you can store the initial version in the model's UserData or Model Workspace and check it in the 'PreSaveFcn' callback:
- Open the Simulink model.
- Set an initial version number using the 'set_param' function:
hws=get_param(modelName,'ModelWorkspace');
assignin(hws,'StoredVersion','1.0');
3. Open Model Properties > Callbacks > PreSaveFcn.
4. Add the following MATLAB code:
userData=get_param(modelName,'ModelWorkspace');
if evalin(hws,'exist(''StoredVersion'',''var'')')
storedVersion=evalin(hws,'StoredVersion');
currentVersion=get_param(modelName,'ModelVersion');
if ~strcmp(storedVersion,currentVersion)
errordlg(sprintf('Saving is disabled because the model version changed.\nStored Version: %s\nCurrent Version: %s', ...
storedVersion,currentVersion),'Save Disabled');
error('Saving is disabled due to version change.');
This will throw an error saying "Saving is disabled due to version change.".
Hope this helps!