Unrecognized function or variable when recalling a function in another script
Show older comments
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
Johan
on 3 Jun 2022
It looks like your fonction doesn't assign Hy in the case where D*w/Vs = pi/2. Either make one of the statement >= or <= or replace elsif by else.
Dyuman Joshi
on 3 Jun 2022
- Hy is not assigned when D*w/Vs is equal to pi/2.
- Is there any use of wD defined in your script?
- You are running a double/nested for loop and only using a scalar array to store values.
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
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)
Answers (1)
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 Loops and Conditional Statements 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!