Can You Explain This Weird Behavior
11 views (last 30 days)
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:
>> Save test flow;
Now in the EDITOR write the following code, save as “testerror”
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)
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)
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)
More Answers (1)
Felix Lauwaert on 21 Aug 2015
I'm not an expert, but try to never use known MATLAB functions. To check if a name is already used, go to command whindow and type:
I checked it for flow and it's something it already exists so MATLAB can get confused sometimes.
NOTE: Anybody who knows it better than me might explain it better, but basically try never to use "existing" names.