MATLAB Answers


Can You Explain This Weird Behavior

Khaled Hamed さんによって質問されました 2015 年 8 月 21 日
最新アクティビティ Khaled Hamed さんによって 編集されました 2015 年 8 月 21 日
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”
load test
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)

  0 件のコメント

サインイン to comment.

2 件の回答

Steven Lord
回答者: Steven Lord
2015 年 8 月 21 日

  1 件のコメント

Khaled Hamed 2015 年 8 月 21 日
Thank you for the explanation. It makes a lot of sense now that you explained it.

サインイン to comment.

回答者: Felix Lauwaert 2015 年 8 月 21 日

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:
which name
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.

  1 件のコメント

Khaled Hamed 2015 年 8 月 21 日
Thank You. At first I did not think this was related to the error. But Steve's answer above explains it all, and it is indeed relevant.

サインイン to comment.

Translated by