如何强制matlab在重载时调用常规函数而不是类方法? [英] How to force matlab to call a regular function rather than class method when they are overloaded?

查看:188
本文介绍了如何强制matlab在重载时调用常规函数而不是类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类 MyClass 的对象 X MyClass 有一个方法 compute ,当我调用 U = compute(X,.. 。),matlab自动调用类的方法。不过,我真正想要的是调用另一个名为 compute 的函数,但其​​参数始于 MyClass 对象。我如何强制matlab调用这个常规函数而不是进入类方法?

Assume I have an object X of class MyClass. MyClass has a method compute, and when I call U = compute(X,...), matlab automatically calls the class method. However, what I actually want is to call another function also called compute whose parameters start with a MyClass object though. How do I force matlab to call this regular function rather than go into the class method?

推荐答案

如果不更改函数的名称或位置,就无法做到这一点。如果您检查 Matlab的函数优先顺序,方法总是正常的外部功能之前运行。您唯一的实用选择是:

There is no way to do this without making some changes either to the function's name or location. If you check Matlab's function precedence order, methods always run before normal external functions. Your only practical options are:


  1. 更改函数名称。 将函数的主体移到调用该函数的相同脚本(上面列表中的项目4)

  2. 将函数的.m文件移动到名为 private 与脚本文件(列表中的第5项)位于同一文件夹中

  1. Change the function's name.
  2. Move the function's body to the same script that is calling the function (item 4 on the list above)
  3. Move the function's .m file to a folder called private in the same folder as your script file (item 5 on the list)

UPDATE

虽然对于较小的项目来说不太实际,但您可能还想查看打包你的功能。一个很好的讨论可以在这个SO贴子中找到。

Although not quite practical for smaller projects, you may also want to look into packaging your functions. A good discussion can be found in this SO post.

这篇关于如何强制matlab在重载时调用常规函数而不是类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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