Unable to iterate my array even though both sides appear to be the same sides.
1 view (last 30 days)
Show older comments
Hey Im trying to create a series of of notes to play in sucessive order. Every time this runs I get the following error.
"Unable to perform assignment because the left and
right sides have a different number of elements.
Error in DSPLab4>createMusic (line 88)
maxtimes(i) = max(Time{1,i});"
However when I use size(maxtimes) and size(Time) they both are 1x4 so I am not sure why they arent the same size. The code is below:
note_vec = [76, 79, 81, 83];
d_vec = [0,5, 0.5, 0.5, 0.5];
start_vec = [0, 0.5, 1, 1.5];
Test = createMusic(d_vec, note_vec, fs, start_vec);
function music = createMusic(d_vect,note_vect,fs,start_vect)
%the arguments
%d_vect the vector of note durations
%note_vect the vector of note numbers
%fs the sampling frequency
%start_vect the vector of starting times eg [1,0,0] means the first note
%starts after 1 second, while the second and third notes start at 0sec.
for k = 1:1:length(note_vect)
[note,time_note] = createNote(d_vect(k),note_vect(k),fs,start_vect(k));
A{k}=note;
Time{k}=time_note;
end
%check the end of each signal
maxtimes = zeros(1,length(note_vect));
for i=1:length(note_vect)
maxtimes(i) = max(Time{1,i});
end
%append zeros only if necessary to time align the signals
maximus = max(maxtimes);
for i=1:length(note_vect)
A{1,i} = [A{1,i};zeros(ceil(maximus)*fs-ceil(maxtimes(i))*fs,1)];
end
%convert cell to matrix
AlmostThere = zeros(ceil(maximus)*fs,length(note_vect));
for i=1:length(note_vect)
AlmostThere(:,i) = A{1,i};
end
%sum columns of the matrix to get the final music
music = sum(AlmostThere,2);
end
2 Comments
Answers (1)
Florian Bidaud
on 21 Nov 2023
Edited: Florian Bidaud
on 21 Nov 2023
Following your comment, you have the answer,
Time{1} is a 0x1 double, so max(Time{1,1}) is empty, therefore you cannot assign it to maxtimes(1)
The issue comes from the function createNote for k = 1
3 Comments
Florian Bidaud
on 21 Nov 2023
Oops sorry I meant 1, was coding in Python just before answering the question :D
I'm correcting the answer now
Dyuman Joshi
on 25 Nov 2023
@Aidan Marrinan, The solution is to include a Fail-safe in your code.
for i=1:length(note_vect)
z = max(Time{1,i});
%Fail safe
if isempty(z)
z=NaN; %Or 0, or any other value you'd like to use as a placeholder
end
maxtimes(i) = z;
end
See Also
Categories
Find more on Get Started with MATLAB 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!