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

1 次查看(过去 30 天)
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;

回答(1 个)

Ben McMahon
Ben McMahon 2021-7-20
编辑:Ben McMahon 2021-7-20
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.

类别

Help CenterFile Exchange 中查找有关 Scope Variables and Generate Names 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by