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

1 view (last 30 days)
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;

Answers (1)

Ben McMahon
Ben McMahon on 20 Jul 2021
Edited: Ben McMahon on 20 Jul 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.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!