MATLAB: Need help abt error.

vector

Here is my code. till the time the alto_dur and teb_dur are exactly same vectors, then there is no problem but if i make
alto_dur = [ 2*t t 2*t t 5*t]; and
treb_dur=[*t* t 2*t t 5*t]; I get error
??? Error using ==> plus Matrix dimensions must agree.
Error in ==> test at 38 final_song = cellfun(@plus, alto, treb, 'Uniform', 0); % Alto Tones and Treble tones are added
The code is here fs=8500; t=.18; % this t is used in duration as a multiple or single identity % Key Number and duration of each note %************************Alto************ alto_keys = [ 46 46 0 46 0]; alto_dur = [ 2*t t 2*t t 5*t]; %************************************
%******************Treeble*********************************** % It is high or acute part of the musical system
treb_keys=[56 56 0 56 0 ]; % Duration of ech note treb_dur=[2*t t 2*t t 5*t]; %********************************************
alto = cell(1, length(alto_keys)); % It will creat a cell array having one row and % columns equal to number of keys for i = 1:length(alto_keys) % The for loop continues till number of Keys alto{i} = adsr_note(alto_keys(i), alto_dur(i)); % Function adsr_note is called which calculates %tone for each key for the specified duration and apply ADSR Envolop on it. This tone is stored in % alto{i} end
treb = cell(1, length(treb_keys)); % It will creat a cell array having one row and % columns equal to number of keys for k = 1:length(treb_keys) % The for loop continues till number of Keys treb{k} = adsr_note(treb_keys(k), treb_dur(k)); % Function adsr_note is called which calculates %tone for each key for the specified duration and apply ADSR Envolop on it. This tone is stored in % treb{i} end
final_song = cellfun(@plus, alto, treb, 'Uniform', 0); % Alto Tones and Treble tones are added % Option Uniform or UniformOutput set to 0 (or false) means, that output from function will be stored in cell array
% Concatinating Tone Vectors tone = cat(2, final_song{:}); % It will concatinate arrays of final_song in a row. 2 means %row. If we will use 1, it will concatinate in a column

Best Answer

Sorry, I just noticed I hadn't changed something. Here is a quick hack with a for loop. I'm sure there is a more elegant way if I had time. Basically you have to figure out which vectors are longer in alto or treb and then pad the shorter vector.
fs=8500; t=.18;
alto_keys = [ 46 46 0 46 0];
alto_dur=[ 3*t t 2*t t 5*t];
treb_keys=[56 56 0 56 0 ];
treb_dur=[3*t t 3*t t 5*t];
alto = cell(1, length(alto_keys));
for i = 1:length(alto_keys) alto{i} = adsr_note(alto_keys(i), alto_dur(i));
end
treb = cell(1, length(treb_keys));
for k = 1:length(treb_keys) treb{k} = adsr_note(treb_keys(k), treb_dur(k)); end
for kk = 1:length(treb)
if (length(treb{kk})==length(alto{kk}))
alto{kk} = alto{kk};
else
alto{kk} = [alto{kk}, zeros(1,length(treb{kk})-length(alto{kk}))];
end
end
final_song = cellfun(@plus, alto, treb, 'Uniform', 0);
tone = cat(2, final_song{:});