Unable to iterate my array even though both sides appear to be the same sides.

5 views (last 30 days)
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));
%check the end of each signal
maxtimes = zeros(1,length(note_vect));
for i=1:length(note_vect)
maxtimes(i) = max(Time{1,i});
%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)];
%convert cell to matrix
AlmostThere = zeros(ceil(maximus)*fs,length(note_vect));
for i=1:length(note_vect)
AlmostThere(:,i) = A{1,i};
%sum columns of the matrix to get the final music
music = sum(AlmostThere,2);
Aidan Marrinan
Aidan Marrinan on 21 Nov 2023
Sure I can include the create note function too:
Here after k = 4
Time = 1×4 cell array
{0×1 double} {48000×1 double} {16000×1 double} {16000×1 double}
The create note function below:
function [x,new_t] = createNote(d, note, fs, start) %the note starts after start seconds
f0 = 440*2^((note-69)/12);
N = ceil(d)*fs;
t = (1/fs)*(0:N-1)';
x = sin(2*pi*f0*t);
prep = zeros(start*fs,1);
x = [prep; x];
new_N = ceil(start+d)*fs;
new_t = (1/fs)*(0:new_N-1)';
if note == -1
x = zeros(N,1);

Sign in to comment.

Answers (1)

Florian Bidaud
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
Florian Bidaud
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
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
maxtimes(i) = z;

Sign in to comment.


Find more on 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!