index exceeds the number of subplots

clc;clear;
f = zeros(13,1);
f(1) = 440;
k = 2^(1/12);
for n = 1:12 f(n+1) = k*f(n);
end
%disp(f)
f_do = f(4);
f_re = f(6);
f_mi = f(8);
f_fa = f(10);
f_so = f(12);
F_s = 16000;
t = 0:1/F_s:1-1/F_s;
do = sin(2*pi*f_do*t);
re = sin(2*pi*f_re*t);
mi = sin(2*pi*f_mi*t);
fa = sin(2*pi*f_fa*t);
so = sin(2*pi*f_so*t);
doremifaso = [do re mi fa so];
subplot(311)
plot(t,do,'r')
xlim([0 0.01])
ylim([-2 2])
subplot(312)
plot(t,re,'b')
xlim([0 0.01])
ylim([-2 2])
subplot(313)
plot(t,mi,'g')
xlim([0 0.01])
ylim([-2 2])
subplot(314)
plot(t,fa,e)
xlim([0 0.01])
ylim([-2 2])
subplot(315)
plot(t,so,s)
xlim([0 0.01])
ylim([-2 2])
soundsc(doremifaso,F_s)
I am trying to write a code that plays the sound of doremifaso. When I plug in this code it says that the "index exceeds the number of subplots" can someone tell me what I am doing wrong

2 Comments

The first subplot(3,1,1) devided the current figure into 3 rows and 1 column and you are trying to plot in rows 4 and 5 that they don't exist.
Try this:
f = zeros(13,1);
f(1) = 440;
k = 2^(1/12);
for n = 1:12
f(n+1) = k*f(n);
end
%disp(f)
f_do = f(4);
f_re = f(6);
f_mi = f(8);
f_fa = f(10);
f_so = f(12);
F_s = 16000;
t = 0:1/F_s:1-1/F_s;
do = sin(2*pi*f_do*t);
re = sin(2*pi*f_re*t);
mi = sin(2*pi*f_mi*t);
fa = sin(2*pi*f_fa*t);
so = sin(2*pi*f_so*t);
doremifaso = [do re mi fa so];
subplot(511)
plot(t,do,'r')
xlim([0 0.01])
ylim([-2 2])
subplot(512)
plot(t,re,'b')
xlim([0 0.01])
ylim([-2 2])
subplot(513)
plot(t,mi,'g')
xlim([0 0.01])
ylim([-2 2])
subplot(514)
plot(t,fa,'k')
xlim([0 0.01])
ylim([-2 2])
subplot(515)
plot(t,so,'s')
xlim([0 0.01])
ylim([-2 2])
soundsc(doremifaso,F_s)
Thanks. I have managed to make it work. I have another question if I may ask, how can I write an FFT analysis for the above wave (do re mi) functions.

Sign in to comment.

Answers (2)

subplot(315)
In the 315, the leading 3 indicates that you want to create an array of plots that has three plots tall. The 1 after that indicates that you want the array of plots to be 1 plot wide. Those two digits together say that you want a 3 x 1 array of plots.
The third digit after that indicates which of the (3 x 1) array you want to select. The plots are numbered top to bottom, left to right, so you start from the top left, move to the right through all the defined columns, then move down to the next row and do that row, and so on. For example if you had a 3 x 2 array of plots, the numbering would be
1 2
3 4
5 6
So with your 3 x 1 array of plots, then numbering of the plots is
1
2
3
That 5 as the third digit says that you want to select the 5th of those plots. But that is a problem, since there are only 3 plot slots defined.
if i change the subplot to (515) does that solve the problem

3 Comments

You will need to change all of the '31' to '51' -- 311, 312, 313, 314, 315
Thanks. I have managed to do it. I have another question, how can write an FFT analysis for the above wave (do re mi) functions.
I suggest you look at the first example in the fft() documentation

Sign in to comment.

Asked:

on 17 Jul 2022

Commented:

on 17 Jul 2022

Community Treasure Hunt

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

Start Hunting!