Can You Explain This Weird Behavior

조회 수: 2 (최근 30일)
Khaled Hamed
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”
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)

채택된 답변

Steven Lord
Steven Lord 2015년 8월 21일
  댓글 수: 1
Khaled Hamed
Khaled Hamed 2015년 8월 21일
Thank you for the explanation. It makes a lot of sense now that you explained it.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Felix Lauwaert
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
Khaled Hamed 2015년 8월 21일
편집: 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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Configure and View Diagnostics에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by