Create table from array in a loop
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (1)
Peter Perkins
le 9 Mar 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.
Catégories
En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
