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 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Walter Roberson
2024년 9월 12일 2:36
편집: Walter Roberson
2024년 9월 12일 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
참고 항목
카테고리
Help Center 및 File Exchange에서 AI for Signals에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!