Error undefined variable or method in script
显示 更早的评论
i don't understand why am i getting this error
i am using matlab online.
clear
clc
my_num = randi(30);
x = input('Enter a number : \n');
guess_the_number(x);
function guess_the_number(n)
if n == my_num
fprintf('Well you guessed it , %d.\n', a)
elseif n > 30
fprintf('Too Large bud, wanna go smaller maybe')
else
fprintf('Not correct, but nice try, number was %d', a)
end
end
Error is this
Enter a number :
12
Unrecognized function or variable 'my_num'.
Error in guess_the_number>guess (line 10)
if n == my_num
Error in guess_the_number (line 6)
guess(x);
回答(1 个)
Ameer Hamza
2020-5-20
编辑:Ameer Hamza
2020-5-20
You need to pass the value of 'a' and 'myNum' to the function
clear
clc
my_num = randi(30);
x = input('Enter a number : \n');
a = 2;
guess_the_number(x, my_num, a);
function guess_the_number(n, my_num, a)
if n == my_num
fprintf('Well you guessed it , %d.\n', a)
elseif n > 30
fprintf('Too Large bud, wanna go smaller maybe')
else
fprintf('Not correct, but nice try, number was %d', a)
end
end
An alternative is to use nested functions.
类别
在 帮助中心 和 File Exchange 中查找有关 Get Started with MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!