Error using extractAfter Numeric value exceeds the number of characters in element 1. What is wrong? How do I fix this?

24 views (last 30 days)
I am trying to eliminate redundant variables from my excel sheet but when I have less than 14 variables I get this error:
Error using extractAfter
Numeric value exceeds the number of characters in element 1.
la(i) = extractAfter(ABAP1(i,1),2)+"^"+extractBetween(ABAP1(i,1),2,2)+"_"+ABAP1(i,2);
%% Set up the Import Options and import the data
opts = spreadsheetImportOptions("NumVariables", 13);
% Specify sheet and range
opts.Sheet = "Sheet2";
opts.DataRange = "A2:M73";
% Specify column names and types
opts.VariableNames = ["Type", "Slice", "Count", "Sphericity", "AvgFeret", "skewness", "kurtosis", "density", "normalizedArea", "totalArea", "avgSize", "zoneArea", "AvgMinFeret"]; %"VarName14", "VarName15", "VarName16", "VarName17", "VarName18", "VarName19"];
opts.VariableTypes = ["string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string"]; % "string", "string", "string", "string", "string", "string"];
% Specify variable propertie
opts = setvaropts(opts, ["Type", "Slice", "Count", "Sphericity", "AvgFeret", "skewness", "kurtosis", "density", "normalizedArea", "totalArea", "avgSize", "zoneArea", "AvgMinFeret"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Type", "Slice", "Count", "Sphericity", "AvgFeret", "skewness", "kurtosis", "density", "normalizedArea", "totalArea", "avgSize", "zoneArea", "AvgMinFeret"], "EmptyFieldRule", "auto");
% Import the data
ABAP1 = readmatrix("excelname.xlsx", opts, "UseExcel", false);
ggmin=1;
ggmax=12;
gg=ggmin:ggmax;
per = 5;
%--------------------------------------
sz = size(ABAP1);
nObs = sz(1);
nVars = sz(2) - 3;
labels = cell(nObs, 1);
for i = 1:nObs
labels{i} = ABAP1(i, 3); % Assuming C2 corresponds to the Type column
end
C = zeros(nObs,nVars);
co = ABAP1(:,1)+"_"+ABAP1(:,2);
coo = ABAP1(:,2);
for i = 1:nObs
for j = 1:nVars
seg(i) = str2double(ABAP1(i,3));
seg2(i) = str2double(extractBetween(co(i),2,2));
la(i) = extractAfter(ABAP1(i,1),2)+"^"+extractBetween(ABAP1(i,1),2,2)+"_"+ABAP1(i,2);
lb(i) = extractAfter(ABAP1(i,1),2)+"_"+ABAP1(i,2);
C(i,:) = str2double(ABAP1(i,4:nVars + 3));
end
% Assign label based on C, W, or E
if ABAP1(i, end) == 'C'
labels{i} = 'C';
elseif ABAP1(i, end) == 'W'
labels{i} = 'W';
elseif ABAP1(i, end) == 'E'
labels{i} = 'E';
end
end
  3 Comments
Chanille
Chanille on 29 May 2023
@Image Analyst I uploaded my datasheet.
for j = 1:nVars
seg(i) = str2double(ABAP1(i,3));
seg2(i) = str2double(extractBetween(co(i),2,2));
la(i) = extractAfter(ABAP1(i,1),2)+"^"+extractBetween(ABAP1(i,1),2,2)+"_"+ABAP1(i,2);
lb(i) = extractAfter(ABAP1(i,1),2)+"_"+ABAP1(i,2);
C(i,:) = str2double(ABAP1(i,4:nVars + 3));
end
What does this snippet mean? I understand that there's alot of extracting and turning to a string but why is my question specifically for which step and why does the la line give error when i remove a column? Specifically what happens when I have to change the numbers in each line?

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 29 May 2023
You are making this much too difficult!
If you want to remove specific variables, first use readmatrix (introduced in R2013b), and then removevars (introduced in R2018a). You are using readmatrix (introduced in R2019a), so you should have all of these functions.
T1 = readtable('exceldata.xlsx')
T1 = 216×12 table
Slice Count Sphericity AvgFeret skewness kurtosis density normalizedArea totalArea avgSize zoneArea AvgMinFeret _____ _____ __________ ________ ________ ________ __________ ______________ _________ _______ __________ ___________ 1 1063 0.47472 16.923 1.4261 6.3692 0.0006312 4.4013 74122 69.729 1.6841e+06 8.4711 2 1169 0.49888 15.938 0.7888 3.3704 0.0013679 8.76 74861 64.038 8.5458e+05 7.909 3 810 0.51745 14.838 0.62505 2.9444 0.0016288 10.552 52477 64.786 4.973e+05 7.7483 4 557 0.5335 15.268 0.3516 2.3852 0.0015122 10.24 37717 67.715 3.6833e+05 8.2089 5 550 0.54636 14.891 0.50214 2.6001 0.0015251 11.005 39687 72.158 3.6062e+05 8.135 6 447 0.54469 14.848 0.42885 2.3657 0.0012531 8.7629 31259 69.931 3.5672e+05 8.1118 7 543 0.53521 14.944 0.70428 2.8828 0.0015328 10.322 36565 67.339 3.5425e+05 8.083 8 578 0.53658 14.326 0.49199 2.4976 0.0016393 10.908 38461 66.542 3.526e+05 7.8097 9 648 0.53308 14.366 0.53685 2.512 0.0018443 12.542 44066 68.003 3.5135e+05 7.6789 10 658 0.51807 14.793 0.57635 2.3995 0.0018771 13.502 47329 71.929 3.5053e+05 7.8666 11 718 0.55385 13.675 0.40135 2.2474 0.0020526 12.903 45135 62.862 3.498e+05 7.1793 12 567 0.53844 14.657 0.44447 2.1955 0.0016232 11.361 39686 69.993 3.4932e+05 7.4717 1 1588 0.49734 15.452 1.0599 4.6982 0.00084496 5.2795 99223 62.483 1.8794e+06 7.7226 2 1126 0.50017 14.493 0.73674 2.9948 0.0017807 9.962 62994 55.945 6.3234e+05 7.2165 3 661 0.52046 15.027 0.60794 2.6089 0.0013816 8.5906 41100 62.179 4.7843e+05 7.7519 4 662 0.55396 13.591 0.65171 2.4891 0.0014232 8.1271 37803 57.104 4.6515e+05 7.3935
See the removevars documentation for details on how to use the function.
.
  11 Comments
Chanille
Chanille on 31 May 2023
@Image Analyst @Star Strider As suggested, here is my full code with readtable but it still is not a working code. Can you please advise:
% Import the entire sheet
ABAP1_full = readtable("HV052923.xlsx", 'Sheet', 'Sheet2');
% Select the range of interest
range = ABAP1_full(2:73, 1:8);
ggmin = 1;
ggmax = 12;
gg = ggmin:ggmax;
per = 5;
sz = size(range);
nObs = sz(1);
nVars = sz(2) - 3;
labels = cell(nObs, 1);
for i = 1:nObs
labels{i} = range.Slice{i};
end
C = zeros(nObs, nVars);
for i = 1:nObs
for j = 1:nVars
C(i, j) = str2double(range{i, j + 2});
end
end
co = range.Slice + "_" + range.Count;
coo = range.Count;
seg = cellfun(@str2double, range.Count);
seg2 = zeros(nObs, 1);
la = strings(nObs, 1);
lb = strings(nObs, 1);
for i = 1:nObs
seg2(i) = str2double(extractBetween(co{i}, 1, 1));
la(i) = extractAfter(range.Slice{i}, 1) + "^" + extractBetween(range.Slice{i}, 1, 1) + "_" + range.Slice{i};
lb(i) = extractAfter(range.Slice{i}, 1) + "_" + range.Slice{i};
end
C = zscore(C);
[coeff,score,latent,tsquared,explained,mu] = pca(C);
size(coeff)

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!