Unrecognized function or variable when recalling a function in another script

3 views (last 30 days)
why I am getting Unrecognized function or variable when recalling a function in another script although when I ran the function by itself, it worked fine.
This is the function
function [Y]=Ya(D,w,Vs,option)
if D*w/Vs<1.1
Hu=cos(D*w/Vs)
elseif D*w/Vs>1.1
Hu=cos(1.1)
else D*w/Vs==1.1
Hu=cos(1.1);
end
if D*w/Vs<pi/2
Hy=.26*(1-cos(D*w/Vs))
elseif D*w/Vs>pi/2
Hy=.26
end
if option == 1
Y = Hu
else option == 2
Y = Hy
end
end
and that is the script in which I am using the function
D = [0,2,5,10,20]
Vs = 600
f = 0:50
w = f*2*pi
for j = 1:5
wD=w.*D(j)/Vs
for i = 1:length(w)
[Y(i)] = Ya(D(j),w(i),Vs,2)
end
plot(f,Y)
end
when I ran the script that what is what it showed
Unrecognized function or variable 'Hy'.
Error in Ya (line 17)
Y = Hy
Error in Test2 (line 9)
[Y(i)] = Ya(D(j),w(i),Vs,2)
  4 Comments
Walter Roberson
Walter Roberson on 3 Jun 2022
else D*w/Vs==1.1
that will calculate whether the condition holds and display the output. Notice that you did not use elseif there. You should probably have commented out the comparison.
Rik
Rik on 3 Jun 2022
Copy of the question in case this one gets edited away as well:
Unrecognized function or variable when recalling a function in another script
why I am getting Unrecognized function or variable when recalling a function in another script although when I ran the function by itself, it worked fine.
This is the function
function [Y]=Ya(D,w,Vs,option)
if D*w/Vs<1.1
Hu=cos(D*w/Vs)
elseif D*w/Vs>1.1
Hu=cos(1.1)
else D*w/Vs==1.1
Hu=cos(1.1);
end
if D*w/Vs<pi/2
Hy=.26*(1-cos(D*w/Vs))
elseif D*w/Vs>pi/2
Hy=.26
end
if option == 1
Y = Hu
else option == 2
Y = Hy
end
end
and that is the script in which I am using the function
D = [0,2,5,10,20]
Vs = 600
f = 0:50
w = f*2*pi
for j = 1:5
wD=w.*D(j)/Vs
for i = 1:length(w)
[Y(i)] = Ya(D(j),w(i),Vs,2)
end
plot(f,Y)
end
when I ran the script that what is what it showed
Unrecognized function or variable 'Hy'.
Error in Ya (line 17)
Y = Hy
Error in Test2 (line 9)
[Y(i)] = Ya(D(j),w(i),Vs,2)

Sign in to comment.

Answers (1)

Sai Teja G
Sai Teja G on 4 Sep 2023
Hi Sam,
I understand that you are not able to run your defined function using another MATLAB script.
The error occurred because you have not assigned a default value to 'Hy' and are using 'if' and 'elseif' conditions to define it. To resolve this issue, you can refer to the code provided below:
if D*w/Vs<pi/2
Hy=.26*(1-cos(D*w/Vs))
elseif D*w/Vs>pi/2 % change this to else or either define default value to Hy
Hy=.26
end
Hope it helps!

Categories

Find more on Text Analytics Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!