Use variable to find column in table
Afficher commentaires plus anciens
I am trying to reset a tables values to 0.
The sceanario may change every time. So I am using table.Properties.VariableNames to find the names of the columns and then I was trying to cycle through the columns using the names stores in table.Properties.VariableNames so I could assign the reset values.
For example, I have a table with 5 columns called A to E
var=table.Properties.VariableNames
var={'A','B','C','D','E'}
table.var(1)=zeros(size(table.var(1)))
The error is that var is not a table column name which is true, but what is stored inside var is. I have to do it like this as one of the columns is actually a mini table and using size(table(:,10)) gives 12 1 but its actually 12 4. using size(table.var10) gives the correct answertab
1 commentaire
Stephen23
le 13 Déc 2023
You can either use T{:,X} or T.(X) to access any variable using expression X.
Réponse acceptée
Plus de réponses (2)
hello
maybe this ? here we want to reset the B column
m = 10,
n = 3;
t = array2table(rand(m,n),'VariableNames',{'A' 'B' 'C'})
var=t.Properties.VariableNames;
col2reset_name = "B";
index = find(contains(var,col2reset_name));
t(:,2) = array2table(zeros(m,1))
Note that as shown on the documentation page linked to by @Stephen23, you can access data in a table using variable names, logical arrays, or subscripts.
A = array2table(magic(4))
B1 = A.Var3
B2 = A{:, "Var3"}
B3 = A{:, 3}
B4 = A{:, [false false true false]}
Converting a subset of a list of variable names in a table to either subscripts (as B3) or a logical mask (as B4) is pretty easy.
varnames = ["Var2", "Var4"]
mask = ismember(A.Properties.VariableNames, varnames)
subscripts = find(mask)
Catégories
En savoir plus sur Tables 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!