Unrecognized function or variable
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (2개)
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.
댓글 수: 0
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!