Here I am facing a problem in output for all solution its showing the same unrecognized variable or name. Not able to proceed further

4 Comments

If you call a function, you must give values to its inputs - in this case "name" and "age" in your call
find_max_age(name,age)
"its showing the same unrecognized variable or name"
You will get this error when you call a function like this:
find_max_age(name,age)
What value should AGE have: 3? 10? 99? How should MATLAB know what value it should use if you do not tell it the value?
I have tired but I am not understanding again I am getting error
"I have tired but I am not understanding again I am getting error"
No, you changed the function definition to something invalid. Change it back to what it was.
I wrote that you need to tell MATLAB the values when you call the function. Lets try a simple example.
Here I call the sine function with an input value of pi/5:
x = pi/5;
sin(x)
ans = 0.5878
Note how x is defined before I call the function SIN.
Now lets try what you are doing (i.e. calling a function with an undefined variable):
sin(y)
Unrecognized function or variable 'y'.
What value does y have? Where is it defined? Answer: nowhere. Result: error.

Sign in to comment.

Answers (1)

Your function defintion was fine originally.
function old_name = find_max_age(name,age)
[~,Id]=max(age)
old_name=name(Id)
end
The problem was that in your Scratch Pad, you did not define any values for the variables name or age before attempting to pass those variables to the find_max_age function.
You have to have something to call the function with, right? For example:
% define some variables:
name = ["Baba","Booey"];
age = [2 9];
% call the function with those variables as inputs:
find_max_age(name,age)
Note that the variables in the Scratch Pad script can have any (valid) name; they don't have to be name and age, i.e., they don't have to match the names of the variables defined in the find_max_age function. For example, this works just as well, without modifying the find_max_age function definition:
% define some variables:
some_names = ["Baba","Booey"];
and_some_ages_too = [2 9];
% call the function with those variables as inputs:
find_max_age(some_names,and_some_ages_too)

Categories

Asked:

on 4 Sep 2024

Moved:

on 4 Sep 2024

Community Treasure Hunt

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

Start Hunting!