Getting "Vectors must be same length" when my vectors are (I believe) the same length.

2 views (last 30 days)
I have seperated my wave file into 10 pieces. I have solved the time and array (sample number) range, but when I attempt to plot these pieces together, it says my vectors aren't the same length. However, as you can be see below, I have printed out the length of every variable and they all agree. I have plotted every part seperately with success but when I put them together in one plot, I get a vector length error. I assume it has something to do with the time or array values intesecting but I haven't been able to reduce down exactly where the error is occuring. The code I am using is also provided below. Thanks in advance for the help.
baseFileName = 'volume_bend_stripped.wav'; % Include extension
fullFileName = fullfile(folder, baseFileName);
[y, Fs] = audioread(fullFileName); % Read into array
%%%%%%%%%%%%%%%
% Audio Split Into 10 Parts
%%%%%%%%%%%%%%
m = n/10;
y1 = y(1:1:(length(y)/10)*1); % Part 1
t1 = (0:1/Fs:(m-1)/Fs);
y2 = y((length(y)/10)*1:1:(length(y)/10)*2-1); % Part 2
t2 = (max(t1):1/Fs:((m*2)-2)/Fs);
y3 = y((length(y)/10)*2:1:(length(y)/10)*3-1); % Part 3
t3 = (max(t2):1/Fs:((m*3)-3)/Fs);
y4 = y((length(y)/10)*3:1:(length(y)/10)*4-1); % Part 4
t4 = (max(t3):1/Fs:((m*4)-4)/Fs);
y5 = y((length(y)/10)*4:1:(length(y)/10)*5-1); % Part 5
t5 = (max(t4):1/Fs:((m*5)-5)/Fs);
y6 = y((length(y)/10)*5:1:(length(y)/10)*6-1); % Part 6
t6 = (max(t5):1/Fs:((m*6)-6)/Fs);
y7 = y((length(y)/10)*6:1:(length(y)/10)*7-1); % Part 7
t7 = (max(t6):1/Fs:((m*7)-7)/Fs);
y8 = y((length(y)/10)*7:1:(length(y)/10)*8-1); % Part 8
t8 = (max(t7):1/Fs:((m*8)-8)/Fs);
y9 = y((length(y)/10)*8:1:(length(y)/10)*9-1); % Part 9
t9 = (max(t8):1/Fs:((m*9)-9)/Fs);
y10 = y((length(y)/10)*9:1:(length(y)/10)*10-1); % Part 10
t10 = (max(t9):1/Fs:((m*10)-10)/Fs);
p1 = plot(t1,y1,'Color',[0.65 0.60 0.21], t2,y2,'Color',[0.67 1.00 0.52],...
t3,y3,'Color',[0.17 1.00 0.52], t4,y4,'Color',[0.39 0.52 0.55], ...
t5,y5,'Color',[0.34 0.92 0.20], t6,y6,'Color',[0.84 0.92 0.20], ...
t7,y7,'Color',[0.77 1.00 0.50], t8,y8,'Color',[0.71 1.00 0.91], ...
t9,y9,'Color',[0.97 0.96 0.71], t10,y10,'Color',[0.02 0.98 0.50]);
disp('Length of time variables')
dl = [length(t1) length(t2) length(t3) length(t4) length(t5) length(t6) length(t7) length(t8) length(t9) length(t10)];
disp(dl)
disp('Length of array variables')
dl1 = [length(y1) length(y2) length(y3) length(y4) length(y5) length(y6) length(y7) length(y8) length(y9) length(y10)];
disp(dl1)

Accepted Answer

Walter Roberson
Walter Roberson on 30 Nov 2022
When you plot() multiple sets of data in the same call, each of the sets can be followed by a linespec that can include line style and color name information . However, named options such as 'color' can only occur after all datasets and corresponding linespec . If there are multiple named options with the same name then only the last one is used.
In short, you cannot provide distinct color triples for each of multiple datasets when you call plot() .
  • you can plot() one at a time, in which case the single 'Color' option would of course apply to that one line; or
  • you can leave out the 'color' option, but record the handles returned in p1, and then set the Color properties of each of the handles individually, such as p1(1).Color = [0.65 0.60 0.21]; p1(2).Color = [0.67 1.00 0.52];
  • you can leave out the 'color' option, but record the handles returned in p1, and then set the Color properties of each of the handles at the same time, using an obscure syntax for set, set(p1, {'Color'}, {[0.65 0.60 0.21]; [0.67 1.00 0.52]; [0.17 1.00 0.52], etc}) -- notice that the property name must be inside {} and that the triples to set must be a cell column vector

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!