Effacer les filtres
Effacer les filtres

Unrecognized function or variable

8 vues (au cours des 30 derniers jours)
Ryan Adams
Ryan Adams le 27 Juil 2020
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

Réponses (2)

Matt J
Matt J le 27 Juil 2020
Modifié(e) : Matt J le 27 Juil 2020
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 le 27 Juil 2020
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 commentaire
Hassan Mehmood Khan
Hassan Mehmood Khan le 29 Juil 2020
Thank you it was helpful for me as well

Connectez-vous pour commenter.

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by