MATLAB: Replace elements in array with smaller array

elements of a big array with smaller arrayfill arrayMATLAB

Hi! I'm having a problem with a matlab script. I have a lot of audio tracks, and ideally they should have the same number of samples. But they don't. Most of them have 912000 samples, but a bunch of them have a little bit more (912043, 912051, etc). The problem is i have to create a matrix using all of this tracks information. So I wrote the next script:
% names of the files:
% MIC01_0_M0_D.wav
% MIC01_0_M10_D.wav
% MIC01_0_M20_D.wav
% MIC01_0_M30_D.wav
%.


%.
%.
% MIC01_0_M350_D.wav
% (the only change is the M index)
[Mic01_0_D, Fs] = audioread('MIC01_0_M0_D.wav'); % upload a random file just to get the number of samples
g_index = (0:10:350); % an array with the steps of M
Mic01_0_D = zeros(length(g_index),length(Mic01_0_D)); %matrix of zeros with "g_indez length" rows and "audiotracks samples lenght" columns.

for i=1:length(g_index)
Mic01_0_D(i,:) = audioread(strcat('MIC01_0_M',num2str(g_index(i)),'_D.wav'));
end
But when the loop reaches the audiotrack with more samples, it obviously crashes (it needs more columns that the ones the matrix already has).
So I tried the next script with the idea of having some extra columns if needed:
[Mic01_0_D, Fs] = audioread('MIC01_0_M0_D.wav'); %upload a random file just to get the number of samples
g_index = (0:10:350); %n array with the steps of M
Mic01_0_D = zeros(length(g_index),length(Mic01_0_D)+1000); %matrix of zeros with "g_indez length" rows and "audiotracks samples lenght" columns.
for i=1:length(g_index)
Mic01_0_D(i,:) = audioread(strcat('MIC01_0_M',num2str(g_index(i)),'_D.wav'));
end
Then, I can take a 36×912000 matrix out of the 36×913000 I get from the loop. I will lost same samples in same tracks but I don't care, as I need just the first 912000. But in this case, the script crashes in the first try, beacause it can't fill a bigger array with a smaller one.
Can anyone please give me a hand with this? Thanks!

Best Answer

Just read into a dummy variable and store only the N samples wanted. Nothing fancy needed at all.
% names of the files:
% MIC01_0_M0_D.wav
% MIC01_0_M10_D.wav
% MIC01_0_M20_D.wav
% MIC01_0_M30_D.wav
Lmax=91200; % Length of record wanted
d=dir('MIC01_0_M*_D.wav'); % return list of files
Nfiles=numel(d); % number files found
MicFiles=zeros(Nfiles,Lmax); % preallocate
for i=1:Nfiles
w=audioread(d(i).name); % upload a random file just to get the number of samples
MicFiles(i,:)=w(1:Lmax); % save Lmax samples
end
Alternatively, you could just use a cell array and save each file's full length; just use the curlies "{}" to dereference the record when reading it back.