I have error "Unrecognized function or variable 'X'" when calling computeCost(X, y, theta)

Hello,
I have code that funtions well when I run it with the "Run button" inside my function computeCost(X, y, theta).
Unfortunately, when I type on the commande line computeCost(X, y, theta) I get the error Unrecognized function or variable 'X'
X, y and theta are well initialized within my code.
computeCost(X, y, theta)
Unrecognized function or variable 'X'.
It is the call to the function in the prompt who raise this error. This doesn't allow me to call the function with other parameters
Can you please help?
Best Regards
Younes

Antworten (2)

You are running a function, this is not the way to call a function. You need to define the variables first and then call the function.
X = yourvariale; % define your variable
x = yourvariable ; % defin your variable
theta = yourvariable ; % define your variable
computeCost(X, y, theta) ;
You need to do the above in a file/ work space where the function is present.

1 Kommentar

Thanks @KSSV for your answer.
Actually, the variables are defined within the function code.
Should I redo this declaration part in the prompt before calling the function?
I'm using Matlab online. Is there a compilation command for myfunction or just saving the script with the function name .mlx and then do the call?
Thanks a lot

Melden Sie sich an, um zu kommentieren.

Hello,
I solved the issue. Thanks for help
In the declaration part, theta was initialized to [0; 0]. It was never updated by the value of the argument entered in the prompt.
I adapted the code, so that the argument replaces the initialized values
Now, it works

Kategorien

Gefragt:

am 10 Feb. 2021

Beantwortet:

am 10 Feb. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by