Index exceeds array bounds despite a loop to prevent this?
function [peak_dat_avg] = FindMuscStrength8chan_Cfs(wavedata,channel,stim_freq,stim_time,lat1,lat2)
artefact_dat = wavedata(:,9,:);
emg_dat = wavedata(:,channel,:);
nframes = size(wavedata,3);
npulse = single(stim_freq*stim_time);
emgpeak_dat = zeros(npulse,1,nframes);
peak_vals = zeros(npulse,1);
for k = 1:nframes
[~, peak_locs] = findpeaks(artefact_dat(:,:,k),’NPeaks’,npulse,’MinPeakProminence’,0.025,’MaxPeakWidth’,5,’MinPeakDistance’,700);
start_idx = round(peak_locs + lat1);
end_idx = round(peak_locs + lat2);
numb_peaks = numel(peak_locs);
for i = 1:numb_peaks
for n = 1:numb_peaks
if (start_idx(n) > 6000)
start_idx(n) = 6000;
end_idx(n) = 6000;
end
end
peak_vals(i) = peak2peak(emg_dat(start_idx(i):end_idx(i),:,k));
end
emgpeak_dat(:,:,k) = peak_vals;
end
peak_dat_avg = mean(nonzeros(emgpeak_dat,1));
end
This function is designed to extract a small window of EMG data after locating a stimulation artefact on channel 9 of the data. The issue comes on line 28 where the error ‘Index in position 1 exceeds array bounds; Index can’t exceed 6000’ pops up. I understand this as when trying to select the window of emg_dat it is attempting to start from a sample higher than 6000. However, I tried to implement the if loop above to locate any index values greater than the range of the data and set them to the maximum. I would really appreciate help on fixing this issuefunction [peak_dat_avg] = FindMuscStrength8chan_Cfs(wavedata,channel,stim_freq,stim_time,lat1,lat2)
artefact_dat = wavedata(:,9,:);
emg_dat = wavedata(:,channel,:);
nframes = size(wavedata,3);
npulse = single(stim_freq*stim_time);
emgpeak_dat = zeros(npulse,1,nframes);
peak_vals = zeros(npulse,1);
for k = 1:nframes
[~, peak_locs] = findpeaks(artefact_dat(:,:,k),’NPeaks’,npulse,’MinPeakProminence’,0.025,’MaxPeakWidth’,5,’MinPeakDistance’,700);
start_idx = round(peak_locs + lat1);
end_idx = round(peak_locs + lat2);
numb_peaks = numel(peak_locs);
for i = 1:numb_peaks
for n = 1:numb_peaks
if (start_idx(n) > 6000)
start_idx(n) = 6000;
end_idx(n) = 6000;
end
end
peak_vals(i) = peak2peak(emg_dat(start_idx(i):end_idx(i),:,k));
end
emgpeak_dat(:,:,k) = peak_vals;
end
peak_dat_avg = mean(nonzeros(emgpeak_dat,1));
end
This function is designed to extract a small window of EMG data after locating a stimulation artefact on channel 9 of the data. The issue comes on line 28 where the error ‘Index in position 1 exceeds array bounds; Index can’t exceed 6000’ pops up. I understand this as when trying to select the window of emg_dat it is attempting to start from a sample higher than 6000. However, I tried to implement the if loop above to locate any index values greater than the range of the data and set them to the maximum. I would really appreciate help on fixing this issue function [peak_dat_avg] = FindMuscStrength8chan_Cfs(wavedata,channel,stim_freq,stim_time,lat1,lat2)
artefact_dat = wavedata(:,9,:);
emg_dat = wavedata(:,channel,:);
nframes = size(wavedata,3);
npulse = single(stim_freq*stim_time);
emgpeak_dat = zeros(npulse,1,nframes);
peak_vals = zeros(npulse,1);
for k = 1:nframes
[~, peak_locs] = findpeaks(artefact_dat(:,:,k),’NPeaks’,npulse,’MinPeakProminence’,0.025,’MaxPeakWidth’,5,’MinPeakDistance’,700);
start_idx = round(peak_locs + lat1);
end_idx = round(peak_locs + lat2);
numb_peaks = numel(peak_locs);
for i = 1:numb_peaks
for n = 1:numb_peaks
if (start_idx(n) > 6000)
start_idx(n) = 6000;
end_idx(n) = 6000;
end
end
peak_vals(i) = peak2peak(emg_dat(start_idx(i):end_idx(i),:,k));
end
emgpeak_dat(:,:,k) = peak_vals;
end
peak_dat_avg = mean(nonzeros(emgpeak_dat,1));
end
This function is designed to extract a small window of EMG data after locating a stimulation artefact on channel 9 of the data. The issue comes on line 28 where the error ‘Index in position 1 exceeds array bounds; Index can’t exceed 6000’ pops up. I understand this as when trying to select the window of emg_dat it is attempting to start from a sample higher than 6000. However, I tried to implement the if loop above to locate any index values greater than the range of the data and set them to the maximum. I would really appreciate help on fixing this issue error, signal processing MATLAB Answers — New Questions