Have a function that can take either string text or numeric input depending on what user is requesting

For reference here is a link that will help with the understanding of my question, So I want to know how to get my code to take in either string text or numeric values and depending if its either one it gives out its corresponding results.
Explanation:
I would also like the code to consider looking for the rest of the carmodels parameters. If instead of carmodel as input I ask for a car with this mileage and horsepower and color, return me it's corresponding battery. All within the same function, is that possible and does this even make sense? Thanks !

댓글 수: 2

You simply define the user interface and code the function as you want -- if it's just a single variable/array, it can be as simple as
function myresult=myfunction(userinput)
if isnumeric(userinput)
...
else
...
end
end
It's all your decision -- how much to try to cram into one function is part of design and factorization -- in general a function should be concise and precise -- do one thing and do it well. The top-level function my rely on many helper functions to achieve its goals but still should be pretty single-minded in its purpose and results. Truly amorphous blobs are very difficult to use/document/develop and even harder to maintain/debug.

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

ML
2022년 5월 19일

댓글:

ML
2022년 5월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by