Evaluation of input arguments

10 views (last 30 days)
Daniel Shub
Daniel Shub on 15 Oct 2012
In answers to this question it is suggested that one cannot prevent evaluation of function inputs. Namely
sin(x)
??? Undefined function or variable 'x'.
This is the same error you get with
x
Is it possible to overload a set of functions which will allow
x
to not throw an error even when x is undefined. It is not clear to me when and how MATLAB evaluates input arguments. What exactly is happening when I type
x

Answers (1)

Matt J
Matt J on 15 Oct 2012
Edited: Matt J on 15 Oct 2012
Bottom line - if you try to pass a variable to a function, the variable must exist in the workspace you're calling the function from.
So, executing 'sin(x)' when x does not exist will not work, because you are trying to send non-existant input data to the SIN function.
When you type 'x' at the command line, something similar happens. MATLAB tries to send x to a function that displays x in the command window. If x does not exist, it obviously cannot be sent to a function, similar to the case of sin(x).
You should probably explain why you think you need to be able to work with undefined variables...
  2 Comments
Daniel Shub
Daniel Shub on 15 Oct 2012
I was more curious if I could, rather than needing to. When you say "MATLAB tries to send x", what do you mean by "MATLAB"? Is it a function that can be overloaded? Is it part of the Java interface, and if so can it be overloaded?
Matt J
Matt J on 15 Oct 2012
I meant "MATLAB" as in the application. The thing that processes your commands.
I should correct what I said before. When you execute a function call in MATLAB, like sin(x), MATLAB first tries to identify the class of x, and then it tries to find a method (in this case SIN) for that class, according to certain rules of precedence. When x does not exist, MATLAB cannot associate the function call with a particular class and so cannot proceed (and gives you an error). Entering 'x' at the command line is the same as entering the function call display(x).
To over-ride this behavior, you would have to over-ride the MATLAB application itself, and the rules it uses to execute expressions. There may be ways to do that, but it's beyond my knowledge.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!