Variables Calculated in Function Somehow Aren't Assigned to Output

조회 수: 1 (최근 30일)
Dominic Troche
Dominic Troche 2021년 7월 21일
편집: Dominic Troche 2021년 7월 21일
My code was working only a day ago, but it just stopped giving me output. I know it calculates the variables I need (wg,pm) becasue it will display them, but it never leaves the scope of the for loop. I know its gotta be something simple, but I'm stumped.
function[wp,pm,wg,gm] = pleaseWork(tf_in)
tf = inline(tf_in,'r')
figure
j = 1
for w = 0:0.1:100
magDB = 20*log10(abs(tf(i*w)));
phase = mod((180/pi)*atan2(imag(tf(i*w)),real(tf(i*w))),-360);
if round(magDB) == 0
wg = w
pm = phase + 180
end
if round(phase) == -180
wp = w
gm = -magDB
end
hold on
subplot(2,1,1)
title('HW 2 #9')
ylabel('Magnitude (dB)')
semilogx(w,20*log10(abs(tf(i*w))),'b.')
hold on
subplot(2,1,2)
ylabel('Phase (deg)')
xlabel('Angular Frequency (rad/s)')
semilogx(w,phase,'b.')
j = j + 1;
end
end
  댓글 수: 2
Torsten
Torsten 2021년 7월 21일
Before entering the for-loop, assign values to wg,pm,wp,gm (e.g. NaN).
What's the relevance of the variable j ?
Dominic Troche
Dominic Troche 2021년 7월 21일
편집: Dominic Troche 2021년 7월 21일
Yeah that was it, I had no idea MATLAB wouldnt output if all outputs weren't assigned. j was accidentally copy and pasted over to this post, I truncated a lot of the code for readability.

댓글을 달려면 로그인하십시오.

채택된 답변

Jonas
Jonas 2021년 7월 21일
편집: Jonas 2021년 7월 21일
looks like your two if statements are never fullfilled, thats why output variables are empty
  댓글 수: 2
Dominic Troche
Dominic Troche 2021년 7월 21일
편집: Dominic Troche 2021년 7월 21일
They're fufilled, as I can get it to display what I need while the function is executing. I just cant get it to output that value.
For example with a transfer function '50/(r*(r+1))'
Dominic Troche
Dominic Troche 2021년 7월 21일
Actually, you're 100% correct. I didnt know MATLAB would assign any outputs if ALL o them werent assigned at some point.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by