How can I configure , so that MATLAB will not allow duplicate built in function by re-definition ?

MATLAB(7.5.0) has some built in functions when some body is Overwriting it by another defination in Matlab Command Window, it is allowing and meaning is changing, Somecases it is giving absurd result & some cases it is giving errors for Overwrite.
for Example:
>> M = magic(3)
M =
8 1 6
3 5 7
4 9 2
>> result = -det(M)
result =
360
>> det =1
det =
1
>> result= -det(M)
??? Index exceeds matrix dimensions.
MATLAB(7.5.0) has some built in functions when some body is Overwriting it by another defination in Matlab Command Window, it is allowing and meaning is changing, Somecases it is giving absurd result & some cases it is giving errors for Overwrite.

 Accepted Answer

I don't know that you can prevent it. Sometimes (rarely) this is exactly what you do want to do. You can make a habit of doing a dependency report and looking it over to see anything that looks like it might be using the wrong function.

More Answers (1)

det is a Matlab function. when you do
det=1
you are creating a new variable det, and shadows the function det. To restore it
clear det
det(M)

6 Comments

yes , it is OK & working perfect,
Is there any configuration entity so that it will not allow to do this instead of writing clear command for MATLAB Built-in function OR it is must to write clear command always?.
Wow, interesting, though if you had to do that for every single function, it would be a nightmare. Best to avoid the need to do that, unless you really did need to run two functions with the same name, a built in one, and one of your own making - though I can't imagine a situation where you'd need to do that instead of picking a custom name, even if it's a wrapper function. Like my wrapper function msgboxw() which is just uiwait(msgbox(message)).
builtin() is needed for OOP, but in my estimation is not usually a good solution for other circumstances.
But practicality wins sometimes. For example if people need a bigger display area on questdlg() and they need to affect existing code and it is expensive to rewrite that code, then they might pop in their own guestdlg() and builtin() within it.
Rewriting is the better approach, but "time is money". And besides, one might need to affect code written by another party, in which case the cost of rewriting over several releases of their software might be discouraging.
yes , not only built in , for user defined function also we are getting same result. Yes, Nobody likes to shadows his own code/variable/function but in a system development a programmer might be defining a user defined function name<xyz> can be changed by another programmer working on same systems by variable name xyz and at the same time the functionality of the function will never work.
So I think it should not be allowed from the compiler itself to the programmer/user.

Sign in to comment.

Categories

Find more on Mathematics 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!