Unrecognized function or variable when recalling a function in another script

3 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2022-6-3
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 2022-6-3
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)

请先登录,再进行评论。

回答(1 个)

Sai Teja G
Sai Teja G 2023-9-4
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!

类别

Help CenterFile Exchange 中查找有关 Text Analytics Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by