Undefined function or variable "sum".

13 visualizzazioni (ultimi 30 giorni)
Craig
Craig il 4 Mar 2016
Commentato: Anthony Dave il 13 Mar 2021
I have the following code:
function outdata=pixelate(fname)
imdata=rgb2gray(imread(fname));
outdata=zeros(size(imdata))+255;
for i=1:size(imdata,1)
indi=[i-1 i i+1];
indi(indi<=0)=[];
indi(indi>size(imdata,1))=[];
isum=sum(indi,:);
isize=length(indi);
for j=1:size(imdata,2)
indj=[i-1 i i+1];
indj(indj<=0)=[];
indj(indj>size(imdata,1))=[];
jsize=length(indj);
avg=sum(isum(indj))/(isize*jsize);
if avg<255*0.5
outdata(i,j)=0;
end
end
end
end
The code analyzer generates a warning on line 8 saying 'The variable 'sum' might be used before it is defined.' The when I run the code I get the following:
Undefined function or variable "sum".
Error in pixelate (line 10)
isum=sum(indi,:);
After a little googling, I tried the following while in debug mode (stopped at line 8):
K>> which sum
built-in (C:\Program Files\MATLAB\R2014a\toolbox\matlab\datafun\@uint8\sum) % uint8 method
K>> which -all sum
built-in (C:\Program Files\MATLAB\R2014a\toolbox\matlab\datafun\@uint8\sum) % uint8 method
built-in (C:\Program Files\MATLAB\R2014a\toolbox\matlab\datafun\@uint16\sum) % Shadowed uint16 method
built-in (C:\Program Files\MATLAB\R2014a\toolbox\matlab\datafun\@uint32\sum) % Shadowed uint32 method
built-in (C:\Program Files\MATLAB\R2014a\toolbox\matlab\datafun\@uint64\sum) % Shadowed uint64 method
built-in (C:\Program Files\MATLAB\R2014a\toolbox\matlab\datafun\@int8\sum) % Shadowed int8 method
built-in (C:\Program Files\MATLAB\R2014a\toolbox\matlab\datafun\@int16\sum) % Shadowed int16 method
built-in (C:\Program Files\MATLAB\R2014a\toolbox\matlab\datafun\@int32\sum) % Shadowed int32 method
built-in (C:\Program Files\MATLAB\R2014a\toolbox\matlab\datafun\@int64\sum) % Shadowed int64 method
built-in (C:\Program Files\MATLAB\R2014a\toolbox\matlab\datafun\@single\sum) % Shadowed single method
built-in (C:\Program Files\MATLAB\R2014a\toolbox\matlab\datafun\@char\sum) % Shadowed char method
built-in (C:\Program Files\MATLAB\R2014a\toolbox\matlab\datafun\@double\sum) % Shadowed double method
built-in (C:\Program Files\MATLAB\R2014a\toolbox\matlab\datafun\@logical\sum) % Shadowed logical method
C:\Program Files\MATLAB\R2014a\toolbox\symbolic\symbolic\@sym\sum.m % Shadowed sym method
C:\Program Files\MATLAB\R2014a\toolbox\matlab\timeseries\@timeseries\sum.m % Shadowed timeseries method
K>> whos sum
K>>
I have tried restarting matlab. I have also tried copying the code and pasting it to a new file. I have not tried restarting the computer, though I will do that when it will not hurt too much.

Risposta accettata

Fangjun Jiang
Fangjun Jiang il 4 Mar 2016
Modificato: Fangjun Jiang il 4 Mar 2016
check the syntax of the sum() function
Syntax
B = sum(A)
B = sum(A,dim)
B = sum(..., 'double')
B = sum(..., dim,'double')
B = sum(..., 'native')
B = sum(..., dim,'native')
Your statement isum=sum(indi,:) is invalid. Maybe you mean isum=sum(indi(:));
  4 Commenti
Adam
Adam il 6 Lug 2017
Modificato: Adam il 6 Lug 2017
The error is a symptom of the, often annoying, similar syntax for calling a function or indexing into a variable, using parenthesis.
Matlab tries to be intelligent and sees the indexing syntax and thus assumes 'sum' is intended as a variable rather than the function, which is why the error occurs and with the wording it has - the variable 'sum' does indeed not exist to be indexed into.
But yes, it is certainly one of those errors that can be very confusing when you aren't used to it or expecting it (and indeed, are we ever expecting such errors?!)
Anthony Dave
Anthony Dave il 13 Mar 2021
Someone code "sum" as a variavle. I rename the var, and it works well.

Accedi per commentare.

Più risposte (1)

Adam
Adam il 4 Mar 2016
Modificato: Adam il 4 Mar 2016
isum=sum(indi,:);
is not valid syntax for the builtin sum function as far as I am aware.
It is a syntax that would be consistent with 'sum' representing a matrix variable which, of course, is undefined, hence the error.

Categorie

Scopri di più su Data Type Identification in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by