How can I fix this error:"Error using matlab.io.datastore.TransformedDatastore/subset Expected indices to be an array with all of the values <= 574."
31 views (last 30 days)
Show older comments
The code is from "Classify Arm Motions Using EMG Signals and Deep Learning"(Classify Arm Motions Using EMG Signals and Deep Learning - MATLAB & Simulink - MathWorks 中国)
clear;clc;
%%
fs = 3000;
localfile = matlab.internal.examples.downloadSupportFile("SPT","data/MyoelectricData.zip");
datasetFolder = fullfile(fileparts(localfile),"MyoelectricData");
if ~exist(datasetFolder,"dir")
unzip(localfile,datasetFolder)
end
sds1 = signalDatastore(datasetFolder,IncludeSubFolders=true,SampleRate=fs);
p = endsWith(sds1.Files,"d.mat");
sdssig = subset(sds1,p);
%%
sds2 = signalDatastore(datasetFolder,SignalVariableNames=["motion";"data_indx"],IncludeSubfolders=true);
p = endsWith(sds2.Files,"i.mat");
sdslbl = subset(sds2,p);
%%
signal = preview(sdssig);
for i = 1:8
ax(i) = subplot(4,2,i);
plot(signal(:,i))
title("Channel"+i)
end
linkaxes(ax,"y")
%%
lbls = {};
i = 1;
while hasdata(sdslbl)
label = read(sdslbl);
idx_start = label{2}(2:end-1)';
idx_end = [idx_start(2:end)-1;idx_start(end)+(3*fs)];
val = categorical(label{1}(2:end-1)',[1 2 3 4 5 6 7], ...
["HandOpen" "HandClose" "WristFlexion" "WristExtension" "Supination" "Pronation" "Rest"]);
ROI = [idx_start idx_end];
% In some cases, the number of label values and ROIs are not equal.
% To eliminate these inconsistencies, remove the extra label value or ROI limits.
if numel(val) < size(ROI,1)
ROI(end,:) = [];
elseif numel(val) > size(ROI,1)
val(end) = [];
end
lbltable = table(ROI,val);
lbls{i} = {lbltable};
i = i+1;
end
%%
lblDS = signalDatastore(lbls);
lblstable = preview(lblDS);
lblstable{1}
%%
DS = combine(sdssig,lblDS);
combinedData = preview(DS)
%%
figure
msk = signalMask(combinedData{2});
plotsigroi(msk,combinedData{1}(:,1))
%%
tDS = transform(DS,@preprocess);
transformedData = preview(tDS)
%%
rng default
[trainIdx,~,testIdx] = dividerand(30,0.8,0,0.2);
trainIdx_all = {};
m = 1;
for k = trainIdx
if k == 1
start = k;
else
start = ((k-1)*24)+1;
end
l = start:k*24;
trainIdx_all{m} = l;
m = m+1;
end
trainIdx_all = cell2mat(trainIdx_all)';
trainDS = subset(tDS,trainIdx_all); %Here is where error appears
testIdx_all = {};
m = 1;
for k = testIdx
if k == 1
start = k;
else
start = ((k-1)*24)+1;
end
l = start:k*24;
testIdx_all{m} = l;
m = m+1;
end
testIdx_all = cell2mat(testIdx_all)';
testDS = subset(tDS,testIdx_all); %Here is where error appears
0 Comments
Answers (1)
Walter Roberson
on 12 Sep 2024 at 2:36
Edited: Walter Roberson
on 12 Sep 2024 at 2:37
tDS happens to have 574 elements.
You are asking to subset it at locations given by testIdx_all
Unfortunately, testIdx_all has entries in it that are more than 574.
[trainIdx,~,testIdx] = dividerand(30,0.8,0,0.2);
so testIdx can contain entries that are as large as 30.
for k = testIdx
%...
l = start:k*24;
trainIdx_all{m} = l;
end
testIdx can be as large as 30 so k can be as large as 30. k*24 could be as large as 30*24 = 720. So you could have recorded values up to 720 in testIdx_all
0 Comments
See Also
Categories
Find more on AI for Signals 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!