Use if in function to check a number is smaller than given number
6 次查看(过去 30 天)
显示 更早的评论
I want to use if to check an input number if the number is less than 10 or not
My code
%%Input
function input (input)
if input<10
disp ('The number is less than 10')
else
disp ('The number is more than 10')
end
No matter what number I enter, the result is always "The number is more than 10", even though the number I entered is less than 10
2 个评论
Stephen23
2023-4-21
编辑:Stephen23
2023-4-22
Do NOT name your function INPUT, or use INPUT as the name of any variable.
After renaming, your code works without error on this forum:
myfun(3)
myfun(13)
function myfun(val)
if val<10
disp('The number is less than 10')
else
disp('The number is more than 10')
end
end
回答(1 个)
Kautuk Raj
2023-6-2
After a quick rename of the variables in your code, it will work as we expect it to. The reason is well explained by @Stephen23 in the comments.
function myfun(val)
if val<10
disp('The number is less than 10')
else
disp('The number is more than 10')
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!