Undefined function or variable 'x'.

3 vues (au cours des 30 derniers jours)
Alexis Riffard
Alexis Riffard le 4 Oct 2016
Hy,
I have a problem, i can't solve with my program. I download a file in mathwoks name shark it is the simulation of an underwater vehicule.
But when i try to run the functions i have this warning message Undefined function or variable 'x'.
this is the function :
function z=vp(x,y)
% z=vp(x,y); z = 3d cross product of x and y
% vp(x) is the 3d cross product matrix : vp(x)*y=vp(x,y).
z=[0 -x(3) x(2);
x(3) 0 -x(1);
-x(2) x(1) 0 ];
if nargin>1, z=z*y; end
  2 commentaires
Joe Yeh
Joe Yeh le 4 Oct 2016
This function runs fine on my machine. How did you call the function ?
Alexis Riffard
Alexis Riffard le 4 Oct 2016
you mean the file ? i name it vp.m

Connectez-vous pour commenter.

Réponses (2)

Mohammad Haghighat
Mohammad Haghighat le 4 Oct 2016
You cannot just "run the function" with no input arguments. It has to be called from another function and the X and Y must be given as its input arguments.

Steven Lord
Steven Lord le 4 Oct 2016
When you call the function, you must specify values for the input arguments listed in the definition of that function. In this case, that definition from the first line of the function is:
function z=vp(x,y)
This function is defined to accept two input arguments (x and y) and return one output argument (z.) Those are the maximum number of inputs and outputs you can specify. (I'm explicitly ignoring varargin and varargout in this discussion for simplicity.)
Specifying fewer output arguments than the function is defined to return is okay unless the author specifically added code to require a certain number of outputs. In this case the author did not add that code. So you can call vp with either 0 or 1 outputs; calling it with 2 will cause an error.
Specifying fewer input arguments than the function is defined to accept can be okay, but the author of the function needs to handle that case in the code itself. In this case the author did add in code to support the case where the user only specific 1 input argument, as the last line of the function. The author did not add code to support the case where the user specified 0 input arguments. So you need to call this function with either 1 or 2 input arguments; calling it with 0 inputs or more than 2 inputs will error.
Based on the error you tried to call it with 0 inputs.
vp
Specify a vector with at least three inputs as the first input argument and it will succeed. For example:
vp([1 2 3]) % or
z = vp([4; 5; 6])

Catégories

En savoir plus sur Argument Definitions dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by