Unrecognized function or variable when recalling a function in another script

3 visualizaciones (últimos 30 días)
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 comentarios
Walter Roberson
Walter Roberson el 3 de Jun. de 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 el 3 de Jun. de 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)

Iniciar sesión para comentar.

Respuestas (1)

Sai Teja G
Sai Teja G el 4 de Sept. de 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!

Categorías

Más información sobre Text Analytics Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by