Undefined function or variable "sum".
显示 更早的评论
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.
采纳的回答
更多回答(1 个)
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!