0

Say I have a class Foo with a method bar.

I also have an independent function bar which takes objects of class Foo.

Is it possible to differentiate the two? Or are they the same?

foo = Foo()
foo.bar() %Is this call equivalent
bar(foo)  %to this call?

Edit: Example of my infinite loop problem:

Project class

classdef Project
...
function predict(proj)
    ...
    run_predict;
end

run_predict (called after "proj = Project()" )

...
predict(proj);

predict function (this is never called!)

function predict(proj);
...
end
Nick Sweet
  • 2,030
  • 3
  • 31
  • 48
  • 1
    Why would they be equivalent? They are entirely unrelated. – Marc Claesen Jul 11 '13 at 19:06
  • How about to try it and find out? – ondrejdee Jul 11 '13 at 19:09
  • I did, and I got stuck in an infinite loop because the class method was always getting called, while the independent function would never get called. I'm wondering how to keep the same function names, but differentiate the function calls, since they're in two separate contexts. – Nick Sweet Jul 11 '13 at 19:45
  • Aha, then it would be great if you could edit your question and produce the minimal example which produces the described behavio (infinite loop). How about that? – ondrejdee Jul 11 '13 at 19:47
  • Added an example! It's fairly minimal, but covers the basics. – Nick Sweet Jul 11 '13 at 19:55
  • 2
    They both will call the class method. This is dup of http://stackoverflow.com/questions/17576506/how-to-force-matlab-to-call-a-regular-function-rather-than-class-method-when-the – Navan Jul 11 '13 at 20:17

1 Answers1

0

If you type which -all bar, you should see something like:

C:\path\to\function\bar.m
C:\path\to\class\Foo.m     % Foo method

Methods are identified with the % Classname method comment.

Or perhaps you were asking something more complex about method/function dispatch?

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
  • That's exactly what I see, however, I can't seem to call the function – it always calls the class method, even if I use the `bar(foo)` syntax. – Nick Sweet Jul 11 '13 at 19:46
  • That's right - class methods always take precedence over functions on the path. I believe that both subfunctions and private functions take precedence over class methods: So in your example, if you make your function `predict` a subfunction of `run_predict` (you'll have to make `run_predict` a function rather than a script to do that) it should get called in preference to the class method. Alternatively make it a private function by creating a directory private in the same directory as `run_predict`, and put the `predict` function there. – Sam Roberts Jul 12 '13 at 17:45
  • ...cont. Note that both the above suggestions will restrict how the `predict` function can be called. If it's a subfunction of `run_predict`, only `run_predict` can call it. If it's a private function, only code within the directory containing the private directory can call it. – Sam Roberts Jul 12 '13 at 17:47