Unrecognized function or variable when recalling a function in another script

조회 수: 4 (최근 30일)
SAM
SAM 2022년 6월 3일
답변: Sai Teja G 2023년 9월 4일
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에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by