Unrecognized function or variable

조회 수: 6 (최근 30일)
Ryan Adams
Ryan Adams 2020년 7월 27일
댓글: Hassan Mehmood Khan 2020년 7월 29일
Hello,
I have a function, greaterthan, that takes two numbers and gives boolean values to if the first is greater than the second. I feel that my coding is correct but it keeps popping up that "greaterthan" is unrecognized. Any suggestions?
function x = greaterthan(a,b)
return
%This function will take two numbers and assign boolean values if
%the first number is greater than the second number.
if a<=b
x = 0;
return
end
if a>b
x = 1
return
end
end

답변 (2개)

Matt J
Matt J 2020년 7월 27일
편집: Matt J 2020년 7월 27일
Well, you should always copy/paste the error message for us, rather than paraphrasing it, so we have the full information. However, if I had to guess, you put your file greaterthan.m in a folder that is not the current folder and which is not on the Matlab path. Change the current folder to the one containing greaterthan.m, and it should be recognized.
Also, get rid of the first "return" statement in the code you've shown.
Also, consider using if...elseif...else...end instead of two separate if...end blocks.

Image Analyst
Image Analyst 2020년 7월 27일
Why is the first line of the function "return"?
function x = greaterthan(a,b)
return
%This function will take two numbers and assign boolean values if
%the first number is greater than the second number.
if a<=b
x = 0;
return
end
if a>b
x = 1
return
end
end
The rest of the function never gets executed after that first return. Get rid of all 3 returns. They're not needed at all. In fact the whole function could simply be this:
function x = greaterthan(a,b)
x = a > b;
end
  댓글 수: 1
Hassan Mehmood Khan
Hassan Mehmood Khan 2020년 7월 29일
Thank you it was helpful for me as well

댓글을 달려면 로그인하십시오.

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by