How to remove the variable “clear” in MATLAB

后端 未结 4 1189
余生分开走
余生分开走 2020-12-18 19:51

Let\'s say you are some new programmer and you do something like...

%...la da da
%...programming away
if such && such
    clear = 1;
else 
    clear          


        
4条回答
  •  自闭症患者
    2020-12-18 20:10

    Any name, even builtin and feval can be overriden. In such case, you can use function handles instead to force MALTAB into interpreting a statement as a function call:

    clear = str2func('clear');
    clear('clear')
    

    Obviously, str2func can also be overrriden! :) however, there exists a similar solution (inspired by Loren's article), which is creating a separate m-file that does the same thing:

    function clearclear()
        assignin('caller', 'clear', @clear);
    

    Calling this function in the main workspace should allow you to do clear('clear') safely.

    The second solution takes advantage of the fact that the m-file doesn't "see" the variable clear in the main workspace, and therefore can access the actual handle of the clear function properly.

提交回复
热议问题