Clear Filters
Clear Filters

Use variable to find column in table

18 views (last 30 days)
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

Accepted Answer

Voss
Voss on 13 Dec 2023
% a table with 5 columns, one of which is a table with 4 columns:
t = table(rand(12,1),rand(12,1), ...
array2table(rand(12,4),'VariableNames',"C"+(1:4)), ...
rand(12,1),rand(12,1), ...
'VariableNames',{'A','B','C','D','E'});
var = t.Properties.VariableNames;
disp(t)
A B C D E C1 C2 C3 C4 ________ ________ ___________________________________________ _______ ________ 0.72865 0.95941 0.87349 0.26227 0.42639 0.96083 0.14744 0.77161 0.85309 0.62403 0.19329 0.28628 0.33 0.6298 0.99507 0.60549 0.73144 0.60098 0.95765 0.69378 0.15803 0.28751 0.31233 0.38058 0.53602 0.59152 0.68489 0.52386 0.18541 0.37659 0.59207 0.49533 0.30399 0.29887 0.8184 0.63234 0.62179 0.76149 0.34043 0.6088 0.011554 0.39662 0.58962 0.064497 0.34494 0.795 0.48311 0.50604 0.66965 0.45485 0.51885 0.47429 0.86962 0.27364 0.80457 0.183 0.34735 0.83887 0.73486 0.1568 0.91418 0.82387 0.66479 0.9985 0.70295 0.027005 0.41849 0.036909 0.61908 0.58419 0.89819 0.27984 0.13258 0.58911 0.86353 0.78146 0.28787 0.16293 0.73759 0.52051 0.4701 0.024021 0.077109 0.80098 0.70586 0.069148 0.78371 0.62528 0.54768 0.15003 0.67575 0.31717 0.29992 0.63877 0.78844 0.041964
Use this syntax to refer to a table column's contents when the column names are stored in var:
t.(var{1})
ans = 12×1
0.7287 0.8531 0.7314 0.5360 0.3040 0.0116 0.6697 0.3474 0.7029 0.1326
t.(var{3})
ans = 12×4 table
C1 C2 C3 C4 ________ ________ _______ ________ 0.87349 0.26227 0.42639 0.96083 0.19329 0.28628 0.33 0.6298 0.95765 0.69378 0.15803 0.28751 0.68489 0.52386 0.18541 0.37659 0.8184 0.63234 0.62179 0.76149 0.58962 0.064497 0.34494 0.795 0.51885 0.47429 0.86962 0.27364 0.73486 0.1568 0.91418 0.82387 0.41849 0.036909 0.61908 0.58419 0.86353 0.78146 0.28787 0.16293 0.077109 0.80098 0.70586 0.069148 0.67575 0.31717 0.29992 0.63877
Using that, here is one way to set everything to 0:
for ii = 1:numel(var)
if istable(t.(var{ii}))
t.(var{ii}){:,:} = 0;
else
t.(var{ii})(:) = 0;
end
end
disp(t)
A B C D E C1 C2 C3 C4 _ _ ____________________ _ _ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  2 Comments
Katherine
Katherine on 4 Jan 2024
This works great and helped made my code more efficient. Your answer was so easy to understand, thank you

Sign in to comment.

More Answers (2)

Mathieu NOE
Mathieu NOE on 13 Dec 2023
hello
maybe this ? here we want to reset the B column
m = 10,
m = 10
n = 3;
t = array2table(rand(m,n),'VariableNames',{'A' 'B' 'C'})
t = 10×3 table
A B C _______ _______ _________ 0.32001 0.97943 0.51794 0.39286 0.79694 0.68753 0.61581 0.71661 0.85918 0.7122 0.33171 0.6402 0.89579 0.68001 0.43428 0.93457 0.24604 0.096676 0.69047 0.23767 0.5885 0.14564 0.79213 0.58002 0.68773 0.32087 0.0058992 0.46555 0.92998 0.89511
var=t.Properties.VariableNames;
col2reset_name = "B";
index = find(contains(var,col2reset_name));
t(:,2) = array2table(zeros(m,1))
t = 10×3 table
A B C _______ _ _________ 0.32001 0 0.51794 0.39286 0 0.68753 0.61581 0 0.85918 0.7122 0 0.6402 0.89579 0 0.43428 0.93457 0 0.096676 0.69047 0 0.5885 0.14564 0 0.58002 0.68773 0 0.0058992 0.46555 0 0.89511

Steven Lord
Steven Lord on 4 Jan 2024
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))
A = 4×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
B1 = A.Var3
B1 = 4×1
3 10 6 15
B2 = A{:, "Var3"}
B2 = 4×1
3 10 6 15
B3 = A{:, 3}
B3 = 4×1
3 10 6 15
B4 = A{:, [false false true false]}
B4 = 4×1
3 10 6 15
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"]
varnames = 1×2 string array
"Var2" "Var4"
mask = ismember(A.Properties.VariableNames, varnames)
mask = 1×4 logical array
0 1 0 1
subscripts = find(mask)
subscripts = 1×2
2 4

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!