如何仅删除由特定matlab脚本创建的变量

Nis*_*ant 3 matlab

有没有办法只删除脚本末尾的matlab脚本中生成的变量,而不删除脚本中未生成的工作空间的任何其他变量?

注意:脚本不是函数.

基本上我想在一行中执行以下操作

save abc.mat    % saves the whole workspace
some_script     % call the script
clear           % deletes the variables created by the script along with the whole workspace
load abc.mat    % again loads the whole earlier workspace
Run Code Online (Sandbox Code Playgroud)

Lui*_*ndo 6

who在脚本之前使用,然后在脚本之后使用; 比较结果(setdiff)来检测脚本中创建的变量,然后clear只检测那些.

变量名varsbefore, varsaftervarsnew在下面的代码应保证不脚本之前或在脚本中使用.

varsbefore = who; %// get names of current variables (note 1)
some_script
varsafter = []; %// initiallize so that this variable is seen by next 'who'
varsnew = []; %// initiallize too.
varsafter = who; %// get names of all variables in 'varsbefore' plus variables 
%// defined in the script, plus 'varsbefore', 'varsafter'  and 'varsnew'
varsnew = setdiff(varsafter, varsbefore); %// variables  defined in the script
%// plus 'varsbefore', 'varsafter'  and 'varsnew'
clear(varsnew{:}) %// (note 2)
Run Code Online (Sandbox Code Playgroud)

关于代码的说明:

  1. who 与输出参数一起使用返回包含所有变量名称的字符串的单元格数组.
  2. 使用函数形式clear,输入参数采用以单元格数组生成的逗号分隔列表的形式.