From: Craig on
Hi All,
I just spent several hours tracking down a code bug caused by the value pi being used as a variable. Is there a way to have matlab through an error if a constant such as pi is reassigned?
Thanks,
Craig
From: Walter Roberson on
"Craig " <cstring625(a)yahoo.com> wrote in message <ht46a1$4ch$1(a)fred.mathworks.com>...

> I just spent several hours tracking down a code bug caused by the value pi being used as a variable. Is there a way to have matlab through an error if a constant such as pi is reassigned?

Sorry, Not as of 2008b.

It is fairly common for a constant to be reassigned -- many programmers use i or j as variable names.
From: Matt Fig on
Just out of curiosity, did you write the code? On this newsgroup folks are constantly telling others not to use words like:

i
j
sum
mean
inv

as variables. I don't know if there is a way to make M-Lint give a warning or not, but good programming practices will go a long way towards averting these types of problems. Perhaps M-Lint needs a service enhancement request?
From: Matt Fig on
This could be of use for cases when some unknown problem is occurring in your code. Take for example, this poor function:


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [] = test_getvars()
% A BAD function.
a = 3;
b = 4;
pi = 5; % No!
R = rand(7);
exp = 3; % No!
randperm = 4; % No!
i = 3; % No!
getvars % Check our ability to stay out of trouble.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

which calls this function.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [] = getvars()
% Find out if one of the variables in a workspace is masking a built-in.
WHOS_OUTPUT_1 = evalin('caller','who');

for ii = 1:length(WHOS_OUTPUT_1)
S = which(WHOS_OUTPUT_1{ii});
if ~isempty(S)
fprintf(['The variable ',WHOS_OUTPUT_1{ii},' may be masking a built-in.\n'])
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


At least it is a starting point :-).
From: TideMan on
On May 21, 8:33 am, "Craig " <cstring...(a)yahoo.com> wrote:
> Hi All,
> I just spent several hours tracking down a code bug caused by the value pi being used as a variable. Is there a way to have matlab through an error if a constant such as pi is reassigned?
> Thanks,
> Craig

Matlab cannot detect acts of stupidity.
Even if it could and gave a warning, would you take any notice?

A few years ago, I learned the hard way that using i as an index was
stupid. I spent hours trying to figure out what was going wrong with
my complex numbers. I never do that now.
There are a few variable names that I never use: i, j, pi and g (which
is usually 9.81).