Why do I get an array out of bounds error
5 views (last 30 days)
Show older comments
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 Comments
Geoff Hayes
on 9 Jun 2019
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.
Accepted Answer
dpb
on 9 Jun 2019
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 Comments
dpb
on 9 Jun 2019
"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...
More Answers (0)
See Also
Categories
Find more on Entering Commands 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!