Error using extractAfter Numeric value exceeds the number of characters in element 1. What is wrong? How do I fix this?
Mostrar comentarios más antiguos
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 comentarios
Image Analyst
el 29 de Mayo de 2023
If you have any more questions, then attach your workbook with the paperclip icon after you read this:
Chanille
el 29 de Mayo de 2023
Chanille
el 29 de Mayo de 2023
Respuestas (1)
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')
See the removevars documentation for details on how to use the function.
.
11 comentarios
Chanille
el 29 de Mayo de 2023
Star Strider
el 29 de Mayo de 2023
My pleasure!
The code you posted is very difficult for me to follow.
Please explain what you want to do.
Chanille
el 29 de Mayo de 2023
Star Strider
el 29 de Mayo de 2023
Editada: Star Strider
el 29 de Mayo de 2023
Please explain what you want to do.
I do not see any "^" or "_" in any of the variable names.
Looking at the values in the table, it would appear that you might want to create a new matrices with different ‘pages’.
Example —
T1 = readtable('exceldata.xlsx')
format shortE
M1 = table2array(T1);
M2 = reshape(M1, 12, [], 12);
M2 = permute(M2,[1 3 2]);
M2(:,:,1)
M2(:,:,2)
M2(:,:,end)
That is obviously optional.
.
Chanille
el 29 de Mayo de 2023
Star Strider
el 29 de Mayo de 2023
Please describe in plain language what you want to do, preferably with at least one specific example.
Note that readmatrix does not read the variable names, however readtable does.
Chanille
el 30 de Mayo de 2023
You can select to read the first seven variables in the readtable call as:
T0 = readtable('exceldata.xlsx', 'Range','A:G')
To read everything and then select the variables:
T1 = readtable('exceldata.xlsx')
... it would be much easier to just do:
V1 = T1{:,1}
V26 = T1{:,2:6}
or equivalently:
V26 = T1{:,[2 3 4 5 6]}
The second option allows you to change the order of the variables in ‘V26’ by changing the order of the column indices inm the ‘T1’ reference.
Of course to do this with the individual reshaped tables (as illustrated in my previous Comment), the addressing would be:
format shortE
M1 = table2array(T1);
M2 = reshape(M1, 12, [], 12);
M2 = permute(M2,[1 3 2]);
M2_17 = M2(:,1:7,:);
M2_17(:,:,1) % Show Page #1
M2_17(:,:,2) % Show Page #2
... and so for the rest.
Indexing into the table or array is much easier than other approaches.
.
Chanille
el 30 de Mayo de 2023
Chanille
el 31 de Mayo de 2023
Image Analyst
el 31 de Mayo de 2023
You forgot to attach "HV052923.xlsx". I'll check back later or tomorrow.
Categorías
Más información sobre Language Fundamentals en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!