MATLAB: Can You Explain This Weird Behavior

MATLABnull function argument

Weird Behavior
I came across some weird behavior while writing an m-file. I have managed to isolate the problem as follows:
In the COMMAND WINDOW write:
>> flow=10;
>> Save test flow;
Now in the EDITOR write the following code, save as “testerror”
clear;
load test
abs(flow(end))
When you run this code, you should simply get the answer 10, but instead you get:
Error using abs
Not enough input arguments.
Error in testerror (line 3)
abs(flow(end))
Copy the code and run it directly by pasting into the COMMAND WINDOW, you get the correct answer, no error!
The following has been noticed:
– This occurs ONLY if the variable name is “flow”! (as far as I tested)
– Occurs only if the keyword “end” is part of the reference to elements of “flow”.
– Occurs only if the code is run from an m-file, but not if run directly from the COMMAND WINDOW.
– Occurs for any function with any number of input arguments.
– Error disappears if the word “flow” appears before the function call (e.g., “f = flow;” or “flow=flow;” or even “flow;”)
Further, in an attempt to check what is actually being sent to the called function, I created a simple function “junk” to receive the input and print “nargin”. The following was observed:
– First, if the function had no output in its declaration line, you get the following error, (when it shouldn’t, since no output is requested):
Error using junk
Too many output arguments.
– If you go along and add an output argument "a" in the declaration line, you get
ans = 0
Error in junk (line 2)
nargin
Output argument "a" (and maybe others) not assigned during call to "junk".
As far as I know, it should not complain about not assigning the output, since no output is requested. However, what is actually important is that “nargin” is always 0, no matter how many inputs are sent to the function. It simply nulls all the arguments of the function. Any explanations?
(Matlab R2013b, Windows 7)

Best Answer

Related Question