Undefined function or variable "sum".

22 ビュー (過去 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 Center および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by