New MATLAB version overrides my function with class method. Can I still call my function?
Asked Answered
N

1

6

I had a function in a file harmonic.m in my matlab path with prototype:

function D = harmonic(A,B,C)

where, importantly, A is expected to be a matrix of type double.

In version r2014a, apparently MATLAB has created a new builtin class method double.harmonic. Thus when I call my function I get an error inside the wrong harmonic. It doesn't help that my harmonic is closer in the path list (which harmonic reveals my path) because my first input is A and harmonic(A,B,C) seems to be equivalent to A.harmonic(B,C).

Is there any way to call my function directly? To ignore this double.harmonic function? I know I can create a function handle from the path, but that's nasty. I'm looking for a more elegant solution or workaround. The most obvious being change my function's name, but then I'll feel bullied : - (.

Neaten answered 2/5, 2014 at 19:51 Comment(2)
I feel your pain: Matlab's forays into class methods keep throwing me for a loop as well.Dermatitis
I'm the author of this question... Apparently, no way to call a function that's shadowed or not on the path directly... Although I do understand that your function is not really shadowed, but the effect is the same...Caribbean
K
2

Put your version of harmonic into a folder @double, and make sure that your folder @double is above \toolbox\symbolic\symbolic\@double on the path (this new double.harmonic is from Symbolic Toolbox).

That will force your function to become a method of double i.e. it will be double.harmonic, rather than a generic function harmonic. When deciding which thing to dispatch to, MATLAB will consider methods first, then generic functions later. Since your double.harmonic and the other one are both methods, and yours is ahead on the path, yours will win. BAM - eat that, MATLAB!

Kerril answered 2/5, 2014 at 22:4 Comment(4)
+1 for the workaround, but this still seems like a short-sighted approach. Overriding built-in functions doesn't seem like a good idea in general. The OP's best option is to probably rename his function, and learn to live with that bullied feeling :) Maybe someday the MathWorks will actually fix the shortcomings of the language and give us things like namespaces that would actually solve these issues.Krakau
@Krakau I know what you're saying. Overriding functions is not a good idea in general. However, it's mostly a problem if you're overriding a function from bas MATLAB, that might be depended on by other parts of the product; this is overriding something from Symbolic Toolbox, which is (mostly) a leaf product, so I think there's less likelihood of a problem.Kerril
@Krakau What do you want from namespaces that packages don't give you? They seem fine for everything I've needed. It's very unfortunate that they can't be used in this case (as the method is on double, which is not in a package), and I think it's kind of antisocial of MathWorks to have implemented harmonic as a method on double rather than as a regular function; but in general, I don't know what more you'd like to see over and above what packages give you.Kerril
What I don't like about packages is that they're inextricably tied to the filesystem. This makes file organization needlessly illogical in some cases. But you could make the same complaint about all functions in MATLAB. When I said namespace support, I meant something that would be contained within the file, and didn't necessarily reflect the path the file resided on. However, I do agree with you that packages are sufficient in most cases.Krakau

© 2022 - 2024 — McMap. All rights reserved.