MATLAB: Are the custom code changes not reflected in the Stateflow model and how to delete the MEX file

stateflow

I have a Simulink model including Stateflow chart, and have some custom code to integrate. I can run it successfully, but when I change my source C code and then re-run the model, the simulation result remains the same.
It seems like the model is still using the MEX file generated at the first time. So I want to delete it, but a window pops up, saying that "The action can't be completed because the file is open in MATLAB".
To delete the file, I have to close and reopen MATLAB. Why is that and is there any better way to delete it?

Best Answer

During the first simulation after you integrate custom code successfully, it runs relatively slow because it takes time to compile and generates a new MEX file. After that, when you re-run the model, it actually uses the generated MEX file which costs less time. If you change the algorithm in your code, you can delete the previously generated MEX file to reflect the changes in your model.
In this case, you can execute
clear mex
to remove the MEX items from workspace and free up memory.
Then you can right-click the MEX file and delete it. When you run the model again, it will generate the new MEX file and show the corresponding simulation result.