等效于evalin,不需要输出参数(内部) [英] equivalent of `evalin` that doesn't require an output argument (internally)

查看:73
本文介绍了等效于evalin,不需要输出参数(内部)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景-我正在阅读有关阴影函数的信息,并开始使用builtin.我写了一个小函数:

Background -- I was reading up on accessing shadowed functions, and started playing with builtin . I wrote a little function:

function klear(x)
%  go to parent environment...
evalin('base', builtin('clear','x')) ;  
end

这会引发错误:

Error using clear
Too many output arguments.

我认为发生这种情况是因为evalin要求从所馈送的内容中获得输出,但是clear是没有返回值的函数之一.
有两个问题:我是否正确地解释了这个问题;如果是,是否有替代函数可以让我在父环境中执行一个函数(不需要输出)?

I think this happens because evalin demands an output from whatever it's being fed, but clear is one of the functions which has no return value.
So two questions: am I interpreting this correctly, and if so, is there an alternative function that allows me to execute a function in the parent environment (that doesn't require an output)?

注意:我完全知道反对尝试访问带阴影的函式(或者避免以重载基本函式等方式命名函数)的论点.这主要是一个问题,可以帮助我了解MATLAB中可以完成和不能完成的工作.

Note: I'm fully aware of the arguments against trying to access shadowed funcs (or rather, to avoid naming functions in a way that overload base funcs, etc). This is primarily a question to help me learn what can and can't be done in MATLAB.

我最初的目标是编写一个需要输入参数的重载函数,以避免clear的恶意软件行为,该行为默认为删除所有内容.在Q& D伪代码中,

My original goal was to write an overload function that would require an input argument, to avoid the malware-ish behavior of clear, which defaults to deleting everything. In Q&D pseudocode,

function clear(x)
if ~exist('x','var') return
execute_in_base_env(builtin(clear(x)))
end

推荐答案

您的clear覆盖有几个问题:

  • 无论从何处调用它,它始终在基本工作区中显示clear.
  • 它不支持多个输入,这是很常见的用例.

相反,我会检查它是否从基本工作区中调用,并以特殊情况检查它是否清除了所有内容.如果某个函数正在调用普通clear来清除其所有变量,则这是一种不好的做法,但这仍然是该函数逻辑的工作方式,并且您不想破坏它.否则可能会出错,或更糟的是,返回错误的结果.

Instead I'd have it check for whether it was called from the base workspace, and special-case that for your check for whether it's clearing everything. If some function is calling plain clear to clear all its variables, that's bad practice, but it's still how that function's logic works, and you don't want to break that. Otherwise it could error, or worse, return incorrect results.

所以,像这样:

function clear(varargin)
  stk = dbstack;
  if numel(stk) == 1 && (nargin == 0 || ismember('all', varargin))
    fprintf('clear: balking at clearing all vars in base workspace. Nothing cleared.\n');
    return;
  end

  % Check for quoting problems
  for i = 1:numel(varargin)
    if any(varargin{i} == '''')
      error('You have a quote in one of your args. That''s not valid.');
    end
  end
  % Construct a clear() call that works with evalin()
  arg_strs = strcat('''', varargin, '''');
  arg_strs = [{'''clear'''} arg_strs];
  expr = ['builtin(' strjoin(arg_strs, ', '), ')'];
  % Do it
  evalin('caller', expr);
end

我希望不言而喻,这是我不建议在实践中使用的骇人听闻的技巧. :)

I hope it goes without saying that this is an atrocious hack that I wouldn't recommend in practice. :)

这篇关于等效于evalin,不需要输出参数(内部)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆