Is it possible to have a workspace variable that persists across a call to clear?

前端 未结 2 1937
忘了有多久
忘了有多久 2020-12-31 07:31

Suppose I run a script X.m and it creates a bunch of variables, and I want to save a variable called Z, so I write myVar = Z.

相关标签:
2条回答
  • 2020-12-31 07:50

    You can use clearvars to clear all variables except specific ones from workspace. From clearvars documentation:

    clearvars -except v1 v2 ... clears all variables except for those specified following the -except flag. Use the wildcard character '' in a variable name to exclude variables that match a pattern from being cleared. clearvars -except X clears all the variables in the current workspace, except for those that start with X, for instance. Use clearvars -except to keep the variables you want and remove all others.

    So, you need to type

    clearvars -except myVars
    

    instead of clear.

    0 讨论(0)
  • 2020-12-31 08:07

    There's Keep on the MATLAB file exchange which lets you clear everything except certain variables that you wish to keep.

    0 讨论(0)
提交回复
热议问题