MATLAB: How to clear locked functions from memory

MATLABmislockedmunlockpersistentsimulinkunlockunlocked

When I start MATLAB and type the following commands at the MATLAB command prompt:
clear functions
inmem
I obtain the following result:
ans =
Empty cell array: 0-by-1
Now if I simulate a model, for example "sf_car", and type the following at MATLAB command prompt:
sim('sf_car')
bdclose all
clear functions
inmem
I recieve the following result while I expect an empty array:
ans =
'simulink\private\slpmloadfunction'
'slsfnagctlr'
'slroot'
'signalselector'
'sigandscopemgr'
'modelrefsiglog'
I want to clear these functions from memory.

Best Answer

The functions remain in memory because they are locked. Use the MISLOCKED function to determine if a function is locked. The locked function can be unlocked using the MUNLOCK function.
For example, if the function FISLOCK is a locked function, type the following commands at MATLAB command prompt, to identify and unlock the function:
mislocked 'fislock' %Checks if fislock is locked
munlock 'fislock' % Do this if the reult for the previous command is 1 or true
clear all; clear functions; %Clears unlocked functions from memory
inmem
Now the function fislock will not appear in the result.
The attached file, clearmemory.m can be run to unlock and remove locked function from memory.
NOTE: These functions may be locked in memory for a reason. Unlocking and clearing can cause other issues. Before clearing a function, ensure that it indeed must cleared.