Unrecognized function or variable

8 次查看(过去 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

回答(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

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by