How to shadow the built-in find function

12 views (last 30 days)
A while ago I have written a FEX submission that extends the capabilities of find to also work for 3D and up: findND (e.g. a syntax like [x,y,z,val]=findND(A)).
I would like to be able to shadow the built-in function, so I have the option of simply using [x,y,z,val]=find(A) in my code. My function would then use the code from this previous question to get a handle to the built-in function, so my function catches the call to find and does its thing if it needs to. Based on this question, I would have thought that it would be relatively easy to do this: just ignore the warning.
However, when I try this, it doesn't work: Matlab still calls the built-in function. list = which('find', '-all') shows my function as the first entry (for R2017b and R2012b).
It makes sense that Matlab would use the built-in, as that is the function precedence order, but in that case I don't understand why which would think that the local function would take precedence.
Is there a way to do this (preferably without requiring the end-user to jump through many hoops), or should I give up and just use my function under its own name?
  3 Comments
Rik
Rik on 5 Mar 2018
Thank you all for your thoughts. Especially KISS and not letting Matlab crash are good ideas ;)
I would still be interested to know what is the reason that which thinks you can overload find, but actual calls to find still use built-in. Seems an odd discrepancy to me.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 5 Mar 2018
Edited: Stephen23 on 5 Mar 2018
I think it would be best to use a different name:
  • KISS: don't make this more complex than it needs to be: defining it as its own function name works perfectly. What is wrong with that?
  • makes it clear that the functionality is different (and it is different, therefore it deserves its own name),
  • makes it clear to everyone that any instances use that special functionality and cannot be replaced by the inbuilt,
  • makes it trivial for any future user to search for that function on the interweb and actually find relevant information (even if they don't know what it is),
  • better "branding" for you: distinctive names and effective code will be remembered and associated with you... whereas awkward hacks that don't work properly will also be associated with you.
  • allows the user to use both functions!
  • any undocumented behavior is hard to replicate. We have no way to know what special cases or overloading any compiled function might have and that might be used by TMW in their own code.
  2 Comments
Jan
Jan on 5 Mar 2018
What about the discrepancy between the first output of which('find'm, -all) and the selected function? Although I'd avoid replacing built-in functions in any case, which should be able to identify the called function.
I had rewritten and shadowed strcmp successfully in R2009a. I edited the code and inserted a bug, which let the function crash. Afterwards I was not able to open the editor, exit failed and after closing Matlab through the task manager I could not start it anymore. Fortunately all I had to do is to delete the new file to repair Matlab, but I'm much more conservative now with shadowing built-in functions.

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!