How to fix a bug with 'sum' function?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
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?
Antworten (4)
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.
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
am 9 Jul. 2015
3 Kommentare
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.)
Walter Roberson
am 12 Okt. 2023
These days, you can get an "undefined function" under the following circumstances:
- You are defining a function; and
- 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
- 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.
0 Kommentare
Siehe auch
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!