Assign NaN to specific rows(based on criteria) for multiple table variables
Show older comments
I want to assign NaN to (time-)table rows that match a criteria (T.Var1~=1). I would like to do this for specific variables in the table.
But instead of doing
T.Var2(T.Var1~=1)=NaN; % Note: My variables are not called Var1, Var2, ... but I simplified it here to these names ;-)
T.Var5(T.Var1~=1)=NaN;
%...
T.Var10(T.Var1~=1)=NaN;
%...
I would like to do this in a shorter code. Is this possible?
I look for something like this:
T.{'Var2','Var5', 'Var7', 'Var10'}(T.Var1~=1)=NaN;
Accepted Answer
More Answers (1)
Benjamin Thompson
on 1 Feb 2022
0 votes
If you are assigning from a scalar it seems you must do this column by column. Use an index vector to select which rows to assign:
T.Var1 = [1 2 3 4 1]'
T.Var2 = 2*ones(5,1)
T.Var3 = 5*ones(5,1)
T.Var4 = 10*ones(5,1)
I = T.Var1 ~= 1
T2.Var2(I) = NaN
T2.Var3(I) = NaN
T2.Var4(I) = NaN
See the MATLAB help article "Access Data in Tables" for more data reading and writing examples.
1 Comment
Marcel345614
on 1 Feb 2022
Categories
Find more on Data Type Conversion 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!