Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
Aidan Marrinan
Aidan Marrinan le 21 Nov 2023
Commenté : Dyuman Joshi le 25 Nov 2023
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 commentaires
Florian Bidaud
Florian Bidaud le 21 Nov 2023
Could you share the variable T after looping on k ?
Aidan Marrinan
Aidan Marrinan le 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);
end
end

Connectez-vous pour commenter.

Réponses (1)

Florian Bidaud
Florian Bidaud le 21 Nov 2023
Modifié(e) : Florian Bidaud le 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 commentaires
Florian Bidaud
Florian Bidaud le 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 le 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by