MATLAB Language Do not name a variable with an existing function name


There is already a function sum(). As a result, if we name a variable with the same name

sum = 1+3;

and if we try to use the function while the variable still exists in the workspace

A = rand(2);

we will get the cryptic error:

Subscript indices must either be real positive integers or logicals.

clear() the variable first and then use the function

clear sum

ans =
       1.0826       1.0279

How can we check if a function already exists to avoid this conflict?

Use which() with the -all flag:

which sum -all
sum is a variable.
built-in (C:\Program Files\MATLAB\R2016a\toolbox\matlab\datafun\@double\sum)   % Shadowed double method

This output is telling us that sum is first a variable and that the following methods (functions) are shadowed by it, i.e. MATLAB will first try to apply our syntax to the variable, rather than using the method.