Undefined function or variable "sum".

조회 수: 12 (최근 30일)
Craig
Craig 2016년 3월 4일
댓글: Anthony Dave 2021년 3월 13일
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.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2016년 3월 4일
편집: Fangjun Jiang 2016년 3월 4일
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
Adam
Adam 2017년 7월 6일
편집: Adam 2017년 7월 6일
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 2021년 3월 13일
Someone code "sum" as a variavle. I rename the var, and it works well.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam
Adam 2016년 3월 4일
편집: Adam 2016년 3월 4일
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.

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by