Filter löschen
Filter löschen

How to fix a bug with 'sum' function?

4 Ansichten (letzte 30 Tage)
Illia
Illia am 9 Jul. 2015
Beantwortet: Walter Roberson am 12 Okt. 2023
Hi all! After installing new version of Matlab (2015a) a strange bug suddenly popped up. The problem with function 'sum'... This it what I have :"Undefined function or variable "sum".". I use it like this: 'sum([packet_data.size])', where packet_data is 1x3 structure. It's really strange, because if I simply run the same command in Command Window it works! But it doesn't work if it is called from script file. Moreover, in all other script files it works also correctly. This script also doesn't work in Matlab 2014a on my computer but it works on all other computers... Has someone any idea what to do with it? Thank you for your comments in advance.
  5 Kommentare
Guillaume
Guillaume am 9 Jul. 2015
What is the rest the error message? It should be followed by 'for arguments of type ...'. Which type is it unhappy about?
Illia
Illia am 9 Jul. 2015
The error is:
Undefined function or variable "sum".
Error in net_el.coordinator/scheduler (line 202)
if ~isempty(packet_data) && sum([packet_data.size]) > 0
The most annoying thing, that if I simply type in Command Window:
sum([packet_data.size]) > 0
it returns me :
ans =
1

Melden Sie sich an, um zu kommentieren.

Antworten (4)

Thorsten
Thorsten am 9 Jul. 2015
Bearbeitet: Thorsten am 9 Jul. 2015
Check
which sum
at the start of your script and immediately before the line that generates the error. Must always be 'builtin ...'.
But even if you have overwritten sum, there should be a different error. It's strange that a built in function generates an 'Undefined function or variable' error.
  2 Kommentare
Illia
Illia am 9 Jul. 2015
Thank you for your hint, but I tried it already. The answer is
built-in (C:\Program Files\MATLAB\R2014a\toolbox\matlab\datafun\@uint8\sum) % uint8 method
203 which sum
built-in (C:\Program Files\MATLAB\R2014a\toolbox\matlab\datafun\@uint8\sum) % uint8 method
204 if ~isempty(packet_data) && sum([packet_data.size]) > 0
Undefined function or variable "sum".
Error in network_elements.MBSFN_area_coordinator/car_FIFO_multicast_scheduler (line 204)
if ~isempty(packet_data) && sum([packet_data.size]) > 0
where the first which in is in the beginning of the script and the second one exactly before the error line. It's really strange...
Thorsten
Thorsten am 9 Jul. 2015
Bearbeitet: Thorsten am 9 Jul. 2015
That strange, I agree. Your code looks perfectly ok to me. Nonetheless, I would try
if ~isempty(packet_data)
which sum
temp = sum([1 2])
temp = sum([packet_data.size]);
if temp > 0
:

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 9 Jul. 2015
Set a breakpoint at the sum line and then type this on the command line. What does it say?
which -all sum
Most likely, you have a variable called "sum" in what you thought was the "irrelevant" part of your script, or you called your script sum.m. Don't do either of those things.
  3 Kommentare
Illia
Illia am 9 Jul. 2015
I do not have any other variable "sum"... and the "which -all sum" gives me
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\distcomp\parallel\@codistributed\sum.m % Shadowed codistributed method
C:\Program Files\MATLAB\R2014a\toolbox\distcomp\gpu\@gpuArray\sum.m % Shadowed gpuArray 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
Image Analyst
Image Analyst am 9 Jul. 2015
Then try
whos sum

Melden Sie sich an, um zu kommentieren.


Illia
Illia am 9 Jul. 2015
Huge thank to all of you for your effort! I fixed it already! Have a good time and thank you again.
  3 Kommentare
Grace Guo
Grace Guo am 12 Okt. 2023
Would you mind sharing how you fixed it? Same error here:(
Steven Lord
Steven Lord am 12 Okt. 2023
@Grace Guo Please show a small segment of code showing your call to the sum function, the definition of the data that you're passing to the function, and the full and exact text of the error message you receive when you run that small segment of code (all the text displayed in red in the Command Window.)

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 12 Okt. 2023
These days, you can get an "undefined function" under the following circumstances:
  1. You are defining a function; and
  2. Somewhere in your function you assign to a variable that has the same name as a function you want to call in a different part of the code; and
  3. at the point in the code you want to call the function, the assignment to the variable with the same name has not taken place yet, or else it took place but you cleared the variable.
That is, these days, MATLAB parses the function definition and sees that you are assigning to a name such as "sum" and decides that must mean that the name ("sum" in this case) must be intended to refer to a variable everywhere inside the function. Then when you get to the point in the code where the name is needed but there is no current variable with that name, it will look in its tables and see that the name has been marked to not be resolved on the search path (because MATLAB assumed it should refer to a variable because you assign to a variable with that name.)
Before this behaviour, whether something referred to a function or a variable was determined strictly dynamically. For example, historically if you had
function test
sum = 0;
clobber_sum();
sum([1 1 1])
end
function clobber_sum
evalin('caller', 'clear', 'sum');
end
then at run-time the clear of sum would take place before the sum([1 1 1]) call, and historically MATLAB would then do name resolution on sum and see that it is a function and would resolve it to the built-in function.
These days, however, MATLAB would see the assignment to sum and would "lock in" the idea that sum is to be a variable and not a function within the body of the function.

Kategorien

Mehr zu Scope Variables and Generate Names finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by