MATLAB: How to rename a Simulink model that is currently open

modelrenamesimulink

I need to rename a Simulink model after it has been saved according to a mask parameter. For example, if a model titled "Car.slx" has a mask parameter "Name" whose value is "House", the model should be renamed as "House.slx" when it's saved. Can I do this using a callback, or by overriding the functionality of the 'save' command?

Best Answer

There is no specific callback that allows to rename the current model in MATLAB R2019b. In MATLAB, there is currently no function to rename files, which means it is necessary to save the system with a new name using the function 'save_system'. This function only creates a new copy of the file with the new name, but it does not delete the old file.
As a workaround, it is possible to use the operating system commands through the MATLAB function 'system'. The windows command for renaming files is 'ren'. Then you need to specify the name of the model and the new name. For example:
> ren oldName.slx newname.slx
Please see the code below for an example on how to implement this in MATLAB:
% First obtain the mask parameter value to use as the new model name
>> oldName = gcs;
>> p = Simulink.Mask.get(gcb);
>> name = p.getParameter('parameterName');
% Construct the system command and execute it
>> command = "ren " + gcs + ".slx " + newName + ".slx";
>> system(command);
For different operating systems such as Mac or Linux, please see their own documentation on how to write these commands. To access the current model, you can use the command 'gcs' as seen in the example above. In the same way, to access the current block with a mask the command 'gcb' can be used. Alternatively, you can specify the name of the model or the block respectively. You can refer to the following pages from our documentation for more information:
The main issue when implementing this workflow, or 'using 'save_system', inside a Model Callback is the fact that the model remains opened when the renaming step takes place. This can cause a conflict since the model opened in Simulink does not get updated and closed. This way, using 'save_system' inside a callback results in an error. At the same time, if you use operating system commands to rename a file within a callback (such as postSave), the file gets renamed correctly. However, the model with the old name remains opened but the actual file does not exist anymore. This would require to close this model and opening the correct renamed one. It is not possible to close or delete the current model from a callback since the model is still under a simulation or saving phase.
To be able to close or delete the model with the old name, you can execute the code above from a script in the MATLAB Command Window. However, this code needs to be execute manually whenever you would like to rename the system. By adding the next two lines of code, you can rename the file, close the current one and open the renamed model:
% Close the current system and open the renamed model
>> bdclose('all')
>> open_system(name)