Unrecognized function or variable 'x'. Error in circular (line 3) if x == 'C' Error in mainapp (line 7) circular;

1 visualizzazione (ultimi 30 giorni)
Hi can someone help me with this code, it is a menu-driven code
THIS IS THE MAIN SCRIPT
choice = ectoption;
while choice ~= 4
switch choice
case 1
explainapp;
case 2
circular;
case 3
triangular;
end
end
THEN THIS IS THE SCRIPT CONNECTED TO IT
function circular
if x == 'C'
r = input("Enter the Radius: ");
disp("Circumference is ");
disp(2*pi*r);
elseif x == 'A'
disp("Area is ");
disp(pi*r*r);
elseif x == 'V'
disp("Volume is ");
disp(4*pi*r*r*r/3);
end
end
the error that keeps on popping out is
Unrecognized function or variable 'x'.
Error in circular (line 3)
if x == 'C'
Error in mainapp (line 7)
circular;

Risposte (1)

Ben McMahon
Ben McMahon il 20 Lug 2021
Modificato: Ben McMahon il 20 Lug 2021
Your issue is that you have not told MATLAB what x is. Your function circular as it is written above has no idea what x, even if it is defined in another function or script as it does not exist in your functions workspace.
A simple solution is to pass x (if it exists elsewhere!) in as a argument to circular for example:
function circular(x)
% your code here
end
If for whatever reason you cannot pass function arguments, study the documentation for assigin.
As an aside, to diagnose errors such as this in the future, try using the debugger in MATLAB. For example you could place a breakpoint near the beginning of the function circular and you should see that x does not appear in the function workspace.

Categorie

Scopri di più su Scope Variables and Generate Names in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by