Error using extractAfter Numeric value exceeds the number of characters in element 1. What is wrong? How do I fix this?
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2023-5-29
If you have any more questions, then attach your workbook with the paperclip icon after you read this:
Chanille
2023-5-29
@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?
回答(1 个)
Star Strider
2023-5-29
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 个评论
Chanille
2023-5-29
@Star Strider Since I am not an advanced coder I am trying not to change the code much. Wouldn't it be easier to explain how to change the string snippet to accomodate the change in variables (i.e. the variables removed)?
Otherwise how would I update the provided snippet to be much easier?
Star Strider
2023-5-29
My pleasure!
The code you posted is very difficult for me to follow.
Please explain what you want to do.
Chanille
2023-5-29
@Star Strider In the original exceldata I had an extra column that was useless so I removed it (i.e. in the provided excel it is not there) but now the following snippet:
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
Does not work and it gives the above error. I changed it to this:
for j = 1:nVars
seg(i) = str2double(ABAP1(i,2));
seg2(i) = str2double(extractBetween(co(i),1,1));
la(i) = extractAfter(ABAP1(i,1),1)+"^"+extractBetween(ABAP1(i,1),1,1)+"_"+ABAP1(i,1);
lb(i) = extractAfter(ABAP1(i,1),1)+"_"+ABAP1(i,1);
C(i,:) = str2double(ABAP1(i,3:nVars + 2));
end
And it now seems to be working but I would like to udnerstand why and if it is correctly assigning based on the provided excel sheet. Can you explain this?
Star Strider
2023-5-29
编辑:Star Strider
2023-5-29
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')
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
format shortE
M1 = table2array(T1);
M2 = reshape(M1, 12, [], 12);
M2 = permute(M2,[1 3 2]);
M2(:,:,1)
ans = 12×12
1.0e+00 *
1.0000e+00 1.0630e+03 4.7472e-01 1.6923e+01 1.4261e+00 6.3692e+00 6.3120e-04 4.4013e+00 7.4122e+04 6.9729e+01 1.6841e+06 8.4711e+00
2.0000e+00 1.1690e+03 4.9888e-01 1.5938e+01 7.8880e-01 3.3704e+00 1.3679e-03 8.7600e+00 7.4861e+04 6.4038e+01 8.5458e+05 7.9090e+00
3.0000e+00 8.1000e+02 5.1745e-01 1.4838e+01 6.2505e-01 2.9444e+00 1.6288e-03 1.0552e+01 5.2477e+04 6.4786e+01 4.9730e+05 7.7483e+00
4.0000e+00 5.5700e+02 5.3350e-01 1.5268e+01 3.5160e-01 2.3852e+00 1.5122e-03 1.0240e+01 3.7717e+04 6.7715e+01 3.6833e+05 8.2089e+00
5.0000e+00 5.5000e+02 5.4636e-01 1.4891e+01 5.0214e-01 2.6001e+00 1.5251e-03 1.1005e+01 3.9687e+04 7.2158e+01 3.6062e+05 8.1350e+00
6.0000e+00 4.4700e+02 5.4469e-01 1.4848e+01 4.2885e-01 2.3657e+00 1.2531e-03 8.7629e+00 3.1259e+04 6.9931e+01 3.5672e+05 8.1118e+00
7.0000e+00 5.4300e+02 5.3521e-01 1.4944e+01 7.0428e-01 2.8828e+00 1.5328e-03 1.0322e+01 3.6565e+04 6.7339e+01 3.5425e+05 8.0830e+00
8.0000e+00 5.7800e+02 5.3658e-01 1.4326e+01 4.9199e-01 2.4976e+00 1.6393e-03 1.0908e+01 3.8461e+04 6.6542e+01 3.5260e+05 7.8097e+00
9.0000e+00 6.4800e+02 5.3308e-01 1.4366e+01 5.3685e-01 2.5120e+00 1.8443e-03 1.2542e+01 4.4066e+04 6.8003e+01 3.5135e+05 7.6789e+00
1.0000e+01 6.5800e+02 5.1807e-01 1.4793e+01 5.7635e-01 2.3995e+00 1.8771e-03 1.3502e+01 4.7329e+04 7.1929e+01 3.5053e+05 7.8666e+00
M2(:,:,2)
ans = 12×12
1.0e+00 *
1.0000e+00 1.5880e+03 4.9734e-01 1.5452e+01 1.0599e+00 4.6982e+00 8.4496e-04 5.2795e+00 9.9223e+04 6.2483e+01 1.8794e+06 7.7226e+00
2.0000e+00 1.1260e+03 5.0017e-01 1.4493e+01 7.3674e-01 2.9948e+00 1.7807e-03 9.9620e+00 6.2994e+04 5.5945e+01 6.3234e+05 7.2165e+00
3.0000e+00 6.6100e+02 5.2046e-01 1.5027e+01 6.0794e-01 2.6089e+00 1.3816e-03 8.5906e+00 4.1100e+04 6.2179e+01 4.7843e+05 7.7519e+00
4.0000e+00 6.6200e+02 5.5396e-01 1.3591e+01 6.5171e-01 2.4891e+00 1.4232e-03 8.1271e+00 3.7803e+04 5.7104e+01 4.6515e+05 7.3935e+00
5.0000e+00 6.9600e+02 5.4988e-01 1.4449e+01 4.9721e-01 2.5070e+00 1.5170e-03 1.0182e+01 4.6714e+04 6.7118e+01 4.5879e+05 7.9909e+00
6.0000e+00 6.8100e+02 5.2378e-01 1.5140e+01 6.3404e-01 2.7618e+00 1.4961e-03 1.0019e+01 4.5607e+04 6.6971e+01 4.5520e+05 8.2886e+00
7.0000e+00 7.9000e+02 5.5748e-01 1.3715e+01 5.8097e-01 2.4220e+00 1.7442e-03 1.0771e+01 4.8785e+04 6.1753e+01 4.5293e+05 7.3978e+00
8.0000e+00 7.3400e+02 5.3429e-01 1.3922e+01 5.6176e-01 2.3041e+00 1.6261e-03 9.7424e+00 4.3977e+04 5.9914e+01 4.5140e+05 7.5124e+00
9.0000e+00 8.5800e+02 5.7099e-01 1.2940e+01 5.7602e-01 2.3872e+00 1.9052e-03 1.1222e+01 5.0536e+04 5.8900e+01 4.5035e+05 7.1497e+00
1.0000e+01 9.0300e+02 5.3019e-01 1.3789e+01 5.7253e-01 2.4163e+00 2.0086e-03 1.2033e+01 5.4097e+04 5.9908e+01 4.4957e+05 7.3559e+00
M2(:,:,end)
ans = 12×12
1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
3 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
4 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
5 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
6 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
7 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
8 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
9 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
10 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
That is obviously optional.
.
Chanille
2023-5-29
@Star Strider This is what I mean:
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?
Star Strider
2023-5-29
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
2023-5-30
I want to read in the first 7 variables in the provided excel sheet so I can use it for the rest of the code. The first variable will be used to label the points and the next 6 will be used to analyze the variables in analysis. How would I do this with the provided snippet?
Star Strider
2023-5-30
You can select to read the first seven variables in the readtable call as:
T0 = readtable('exceldata.xlsx', 'Range','A:G')
T0 = 216×7 table
Slice Count Sphericity AvgFeret skewness kurtosis density
_____ _____ __________ ________ ________ ________ __________
1 1063 0.47472 16.923 1.4261 6.3692 0.0006312
2 1169 0.49888 15.938 0.7888 3.3704 0.0013679
3 810 0.51745 14.838 0.62505 2.9444 0.0016288
4 557 0.5335 15.268 0.3516 2.3852 0.0015122
5 550 0.54636 14.891 0.50214 2.6001 0.0015251
6 447 0.54469 14.848 0.42885 2.3657 0.0012531
7 543 0.53521 14.944 0.70428 2.8828 0.0015328
8 578 0.53658 14.326 0.49199 2.4976 0.0016393
9 648 0.53308 14.366 0.53685 2.512 0.0018443
10 658 0.51807 14.793 0.57635 2.3995 0.0018771
11 718 0.55385 13.675 0.40135 2.2474 0.0020526
12 567 0.53844 14.657 0.44447 2.1955 0.0016232
1 1588 0.49734 15.452 1.0599 4.6982 0.00084496
2 1126 0.50017 14.493 0.73674 2.9948 0.0017807
3 661 0.52046 15.027 0.60794 2.6089 0.0013816
4 662 0.55396 13.591 0.65171 2.4891 0.0014232
To read everything and then select the variables:
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
... it would be much easier to just do:
V1 = T1{:,1}
V1 = 216×1
1
2
3
4
5
6
7
8
9
10
V26 = T1{:,2:6}
V26 = 216×5
1.0e+03 *
1.0630 0.0005 0.0169 0.0014 0.0064
1.1690 0.0005 0.0159 0.0008 0.0034
0.8100 0.0005 0.0148 0.0006 0.0029
0.5570 0.0005 0.0153 0.0004 0.0024
0.5500 0.0005 0.0149 0.0005 0.0026
0.4470 0.0005 0.0148 0.0004 0.0024
0.5430 0.0005 0.0149 0.0007 0.0029
0.5780 0.0005 0.0143 0.0005 0.0025
0.6480 0.0005 0.0144 0.0005 0.0025
0.6580 0.0005 0.0148 0.0006 0.0024
or equivalently:
V26 = T1{:,[2 3 4 5 6]}
V26 = 216×5
1.0e+03 *
1.0630 0.0005 0.0169 0.0014 0.0064
1.1690 0.0005 0.0159 0.0008 0.0034
0.8100 0.0005 0.0148 0.0006 0.0029
0.5570 0.0005 0.0153 0.0004 0.0024
0.5500 0.0005 0.0149 0.0005 0.0026
0.4470 0.0005 0.0148 0.0004 0.0024
0.5430 0.0005 0.0149 0.0007 0.0029
0.5780 0.0005 0.0143 0.0005 0.0025
0.6480 0.0005 0.0144 0.0005 0.0025
0.6580 0.0005 0.0148 0.0006 0.0024
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
ans = 12×7
1.0e+00 *
1.0000e+00 1.0630e+03 4.7472e-01 1.6923e+01 1.4261e+00 6.3692e+00 6.3120e-04
2.0000e+00 1.1690e+03 4.9888e-01 1.5938e+01 7.8880e-01 3.3704e+00 1.3679e-03
3.0000e+00 8.1000e+02 5.1745e-01 1.4838e+01 6.2505e-01 2.9444e+00 1.6288e-03
4.0000e+00 5.5700e+02 5.3350e-01 1.5268e+01 3.5160e-01 2.3852e+00 1.5122e-03
5.0000e+00 5.5000e+02 5.4636e-01 1.4891e+01 5.0214e-01 2.6001e+00 1.5251e-03
6.0000e+00 4.4700e+02 5.4469e-01 1.4848e+01 4.2885e-01 2.3657e+00 1.2531e-03
7.0000e+00 5.4300e+02 5.3521e-01 1.4944e+01 7.0428e-01 2.8828e+00 1.5328e-03
8.0000e+00 5.7800e+02 5.3658e-01 1.4326e+01 4.9199e-01 2.4976e+00 1.6393e-03
9.0000e+00 6.4800e+02 5.3308e-01 1.4366e+01 5.3685e-01 2.5120e+00 1.8443e-03
1.0000e+01 6.5800e+02 5.1807e-01 1.4793e+01 5.7635e-01 2.3995e+00 1.8771e-03
M2_17(:,:,2) % Show Page #2
ans = 12×7
1.0e+00 *
1.0000e+00 1.5880e+03 4.9734e-01 1.5452e+01 1.0599e+00 4.6982e+00 8.4496e-04
2.0000e+00 1.1260e+03 5.0017e-01 1.4493e+01 7.3674e-01 2.9948e+00 1.7807e-03
3.0000e+00 6.6100e+02 5.2046e-01 1.5027e+01 6.0794e-01 2.6089e+00 1.3816e-03
4.0000e+00 6.6200e+02 5.5396e-01 1.3591e+01 6.5171e-01 2.4891e+00 1.4232e-03
5.0000e+00 6.9600e+02 5.4988e-01 1.4449e+01 4.9721e-01 2.5070e+00 1.5170e-03
6.0000e+00 6.8100e+02 5.2378e-01 1.5140e+01 6.3404e-01 2.7618e+00 1.4961e-03
7.0000e+00 7.9000e+02 5.5748e-01 1.3715e+01 5.8097e-01 2.4220e+00 1.7442e-03
8.0000e+00 7.3400e+02 5.3429e-01 1.3922e+01 5.6176e-01 2.3041e+00 1.6261e-03
9.0000e+00 8.5800e+02 5.7099e-01 1.2940e+01 5.7602e-01 2.3872e+00 1.9052e-03
1.0000e+01 9.0300e+02 5.3019e-01 1.3789e+01 5.7253e-01 2.4163e+00 2.0086e-03
... and so for the rest.
Indexing into the table or array is much easier than other approaches.
.
Chanille
2023-5-30
Thanks I will try this, oh, given the snippet i provided how would i adjust it to accommodate the readtable function as you illustrated?
Chanille
2023-5-31
@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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)