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

2 次查看(过去 30 天)
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 个评论
Aidan Marrinan
Aidan Marrinan 2023-11-21
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

请先登录,再进行评论。

回答(1 个)

Florian Bidaud
Florian Bidaud 2023-11-21
编辑:Florian Bidaud 2023-11-21
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 个评论
Florian Bidaud
Florian Bidaud 2023-11-21
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 2023-11-25
@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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by