Create table from array in a loop

23 visualizaciones (últimos 30 días)
CB
CB el 8 de Mzo. de 2022
Respondida: Peter Perkins el 9 de Mzo. de 2022
Hi all,
I am working on code that is able to read two columns of data (Diameter and height ) belonging to various Excel spreadsheets pertaining to collected data so that these can be grouped in 5mm groups according to a Diameter values.
My first approach was to produce a specific code for each project as described below, so that in the end I could work from the data populated as per table Vtotal:
rawTable = readtable('20220209_Diameter.xlsx','Sheet','ExportResult');
x = rawTable.x; %: Column x
y = rawTable.y; %: Column y
V835 = rawTable((rawTable.x > 830) & (rawTable.x <= 835),:);
V835M = table2array(V835(:,2:3));
V835M(:,3)=835;
V835N = array2table(V835M,'VariableNames',{'x','y','z'});
V840 = rawTable((rawTable.x > 835) & (rawTable.x <= 840),:);
V840M = table2array(V840(:,2:3));
V840M(:,3)=840;
V840N = array2table(V840M,'VariableNames',{'x','y','z'});
Vtotal = [V835N; V840N; V845N; V850N; V855N; V860N; V865N; V870N; V875N; V880N];
Xtotal = Vtotal.z;
Ytotal = Vtotal.y;
boxplot(Ytotal, Xtotal)
I would now be looking to create a standard code that is able to read any set of data belonging to the different spreadsheets mentioned above, so have produced the following code.
range=5;
rawTable = readtable('20220209_Diameter.xlsx','Sheet','ExportResult');
x = rawTable.x; %: Column x
y = rawTable.y; %: Column y
Diam_min=790; % Add minimum diameter
Diam_max=860; % Add maximum diameter
n_div=(Diam_max-Diam_min)/range;
Diam=x;
Sh=y;
for j=1:n_div
V = rawTable((rawTable.x > (Diam_min+(j-1)*range)) & (rawTable.x <= (Diam_min+j*range)),:);
VM = table2array(V(:,2:3));
VM(:,3)=Diam_min+j*range-range/2; % Valor en el que centramos el boxplot en "X"
VN = array2table(VM,'VariableNames',{'x','y','z'});
end
What I require, is the above Vtotal table to be populated after each iteration so that I get the same sort of data as above and bearing in mind that the data amount and grouping will vary between spreadsheets.

Respuesta aceptada

Stephen23
Stephen23 el 8 de Mzo. de 2022
Editada: Stephen23 el 8 de Mzo. de 2022
"to collected data so that these can be grouped in 5mm groups according to a Diameter values."
The MATLAB approach would be to use some simple, very efficient mathematics. For example:
X = 800+23*rand(9,1);
Y = 32*rand(9,1);
T = table(X,Y) % your input data
T = 9×2 table
X Y ______ ______ 821.92 10.806 807.72 6.1337 820.14 2.1696 817.15 3.2259 804.68 9.4653 820.68 12.7 801.18 9.8082 813 1.2345 809.27 18.478
T.G = 5*ceil(T.X/5)
T = 9×3 table
X Y G ______ ______ ___ 821.92 10.806 825 807.72 6.1337 810 820.14 2.1696 825 817.15 3.2259 820 804.68 9.4653 805 820.68 12.7 825 801.18 9.8082 805 813 1.2345 815 809.27 18.478 810
boxplot(T.Y,T.G)
Vtotal = sortrows(T,'G')
Vtotal = 9×3 table
X Y G ______ ______ ___ 804.68 9.4653 805 801.18 9.8082 805 807.72 6.1337 810 809.27 18.478 810 813 1.2345 815 817.15 3.2259 820 821.92 10.806 825 820.14 2.1696 825 820.68 12.7 825
  3 comentarios
CB
CB el 8 de Mzo. de 2022
Hi Stephen,
Thank you so much.
kind regards,
Cesar
Stephen23
Stephen23 el 8 de Mzo. de 2022
@CB: another option would be to use DISCRETIZE:
which would allow you to define the bin edges as a simple numeric vector.

Iniciar sesión para comentar.

Más respuestas (1)

Peter Perkins
Peter Perkins el 9 de Mzo. de 2022
In addition to what Stephen has said, this code (which as Stephen points out was unncessary)
V835 = rawTable((rawTable.x > 830) & (rawTable.x <= 835),:);
V835M = table2array(V835(:,2:3));
V835M(:,3)=835;
V835N = array2table(V835M,'VariableNames',{'x','y','z'});
could have been much simpler. I would think something like this:
V835N = rawTable((rawTable.x > 830) & (rawTable.x <= 835),["x" "y"]);
V835.z(:) = 835;
People tend to overuse table2array/array2table. Often just a dot works.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by