Can You Explain This Weird Behavior

2 vues (au cours des 30 derniers jours)
Khaled Hamed
Khaled Hamed le 21 Août 2015
Modifié(e) : Khaled Hamed le 21 Août 2015
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)

Réponse acceptée

Steven Lord
Steven Lord le 21 Août 2015
  1 commentaire
Khaled Hamed
Khaled Hamed le 21 Août 2015
Thank you for the explanation. It makes a lot of sense now that you explained it.

Connectez-vous pour commenter.

Plus de réponses (1)

Felix Lauwaert
Felix Lauwaert le 21 Août 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:
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 commentaire
Khaled Hamed
Khaled Hamed le 21 Août 2015
Modifié(e) : Khaled Hamed le 21 Août 2015
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Function Creation dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by