Why do I get an array out of bounds error
조회 수: 5 (최근 30일)
이전 댓글 표시
Hello,
I am attempting to implement a wah-wah effect in the app designer and get an index out of bounds error when running this code:
function out = ApplyWahWah(app, out)
t = [1:length(out)*5] * (1/app.Fs);
t = t(:);
LFO = sin(20 * t)*200;
channel = out;
for n = 1:length(out)
if n-fix(LFO(n)) > 1
out(n) = out(n) + channel(n-fix(LFO(n)));
end
end
end
The error is
Index exceeds the number of array elements (1338204)
And apparently it is because LFO doesn't have n items.
However, as seen in the code, I create LFO with a length, than n does not exceed. Also, this code works fine when I run it in a .m script, but fails when doing it in an app. What on eart is happening? I exported the app as a .m file and it did not work there either, but I have pasted the entire code here: https://pastebin.com/T13j1rDF - The t= [1:length(out)*5] is something I added to try and fix it, but it didn't work. Howver, it should not make any difference.
I really hope you can help me, because I am lost. Thanks a lot :-)
댓글 수: 2
Geoff Hayes
2019년 6월 9일
Niels - can you copy and paste the full error message to this question? Since LFO is based on t which is based on out, then all three arrays should have the same dimensions. Can you tell us what the dimensions for out are? What is app.Fs set to? You may need to put a breakpoint in your code and then run the app to see what is being assigned to all of these variables (including t) so that you can get idea of whay may be going wrong.
채택된 답변
dpb
2019년 6월 9일
out(n) = out(n) + channel(n-fix(LFO(n)));
the subscript for channel is n-fix(LFO(n)) where LFO=200*sin(t) so fix(LFO) ranges from [-200, 200](*) and then (n-(-fix(LFO)) -->n+200.
(*) Theoretically, probably never actually makes 200 so is +/-199 instead, but conclusion is the same...you're addressing elements beyond n.
댓글 수: 2
dpb
2019년 6월 9일
"This resolved the issue. Thanks for your help :-)"_
If resovled, please ACCEPT Answer to indicate to others have satisfactory solution to other if for no other reason...
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!