Why do I get this Error using . "Array indices must be positive integers or logical values."
1 view (last 30 days)
Show older comments
This is my code:
%%
%extract files that have '21' in them
files = dir("*21*");
S = struct;
figure(1);clf
t1 = tiledlayout('flow');
for k = 1:length(files)
T_name = ['Exp_' + string(files(k).name)];
S.(T_name) = readtable(files(k).name);
nexttile
plot(S.(T_name).Time_s_, S.(T_name).AI0HPF_V_)
title(strrep(T_name, '_', ' '));
axis([-inf, inf, -1.5, 1.5]);
clear T_name
end
t1.Padding = 'compact';
t1.TileSpacing = 'compact';
%%
T_qemg = table;
T_qemg.lb_0 = NaN(5,1);
T_qemg.lb_5 = NaN(5,1);
T_qemg.lb_10 = NaN(5,1);
T_qemg.lb_15 = NaN(5,1);
T_qemg.lb_20 = NaN(5,1);
names2 = fieldnames(S);
for b = 1:numel(names2)
REMG = S.(string(names2(b))).AI0HPF_V_;
rec_EMG = abs(REMG(3000:5000));
RMS = sqrt(movmean(rec_EMG.^2,200));
%S.(string(names2(b))).RMS = RMS;
QEMG = mean(RMS);
%variance = var(REMG(2000:4000));
%QEMG = mean(variance);
parts = strsplit(string(names2(b)),'_');
weight = str2double(parts(3));
row = str2double(parts(4));
if parts(3) == "0"
T_qemg.lb_0(row)=QEMG;
elseif parts(3) == "5"
T_qemg.lb_5(row)=QEMG;
elseif parts(3) == "10"
T_qemg.lb_10(row)=QEMG;
elseif parts(3) == "15"
T_qemg.lb_15(row)=QEMG;
elseif parts(3) == "20"
T_qemg.lb_20(row)=QEMG;
else
fprintf("data error in file names")
end
clear REMG variance QEMG parts row weight
end
figure(2);clf
p= plot(table2array(T_qemg)','o');
And this is the error message I can't seem to get past:
Error using .
Array indices must be positive integers or logical values.
Error in tutorialplots (line 72)
T_qemg.lb_0(row)=QEMG;
The first section of code runs fine, it's only second section I'm struggling with. "T_name" is of the form "Exp_21_5_e" so "parts" is a 1x4 string.
0 Comments
Answers (1)
Walter Roberson
on 29 Oct 2023
The code splits apart the name at '_" characters, so for the name "Exp_21_5_e" parts(1) would be "Exp", parts(2) would be "21", parts(3) would be "5" and parts(4) would be 'e'
It then does a str2double(parts(4)) would attempts to convert the string 'e' to a number. The result of that is going to be NaN.
It then tries to use that NaN as a row index.
You should be check isnan(Row) and if so do something appropriate.
0 Comments
See Also
Categories
Find more on Data Type Conversion 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!