how to concatenate two audio files of different matrix dimensions?

21 views (last 30 days)
Hi all,
this is my code-
[y1,Fs] = audioread('M:\audio files\british.wav');
len= length(y1);
[y2,Fs] = audioread('M:\audio files\door1.wav');
q=44100; %sampling rate of y1
% Extract first 10second samples from y1,
% Then attach y2,
% Then extract the remaining samples
% and stick them on the end.
y3 = [y1(1:q*10,:); y2; y1(q*10+1:end)];
audiowrite('M:\audio files\british.wav', y3,Fs);
the error message which i get is-
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in Untitled2 (line 13)
y3 = [y1(1:q*10,:); y2; y1(q*10+1:end)];
can anyone please help me with this problem?

Accepted Answer

Guillaume
Guillaume on 20 Nov 2018
You're missing the column indexing in your y1(q*10+1:end), therefore it's converted into a row vector, which will have a different number of columns than y2 and y1(1:q*10,:). The correct code:
y3 = [y1(1:q*10,:); y2; y1(q*10+1:end, :)]; %that last colon is critical
Of course, y2 must have the same numbers of channels as y1 for the above to work.

More Answers (1)

madhan ravi
madhan ravi on 20 Nov 2018
To perform this operation size(y1(1:q*10,:)) size(y2) and size(y1(q*10+1:end)) have to be the same - imagine if a matrix has unequal rows it's not a mtrix right?
y3 = [y1(1:q*10,:); y2; y1(q*10+1:end)];

Community Treasure Hunt

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

Start Hunting!