Use evalinwith option 'caller'. That is, replace the line
builtin('clear')
by
evalin('caller', 'builtin(''clear'')')
This will clear all variables from the workspace of the calling function.
If you want to remove all variables from the workspace of the Matlab base, use the parameter 'base':
evalin('base', 'builtin(''clear'')')
source
share