Borrar filtros
Borrar filtros

Unrecognized function or variable

8 visualizaciones (últimos 30 días)
Ryan Adams
Ryan Adams el 27 de Jul. de 2020
Comentada: Hassan Mehmood Khan el 29 de Jul. de 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

Respuestas (2)

Matt J
Matt J el 27 de Jul. de 2020
Editada: Matt J el 27 de Jul. de 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 el 27 de Jul. de 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

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by