Use if in function to check a number is smaller than given number

5 ビュー (過去 30 日間)
non non
non non 2023 年 4 月 21 日
回答済み: Kautuk Raj 2023 年 6 月 2 日
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
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)
The number is less than 10
myfun(13)
The number is more than 10
function myfun(val)
if val<10
disp('The number is less than 10')
else
disp('The number is more than 10')
end
end
Rik
Rik 2023 年 4 月 22 日
I would not have expected this code to run at all. Which release are you using?

サインインしてコメントする。

回答 (1 件)

Kautuk Raj
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

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by