tables remove one field
1 次查看(过去 30 天)
显示 更早的评论
I have three fields in a table
T.Position= 'No' '10' '20'
T.Velocity= 'No' '10' '60'
T.Acceleration= 'No' 'No' 'No'
then I want to eliminate the field that have all values 'No' which is only Acceleration
How I can do it??
Thanks
0 个评论
回答(3 个)
Chibuzo Nnonyelu
2017-2-23
Goes through each column and deletes a column if the number of 'No' found in the column is equal to the height of the table.
for m = 1:width(T)
if length(strfind(strjoin(table2cell(T(:, m))), 'No') ) == height(T)
% delete the column;
T(:, m) = [];
end
end
Peter Perkins
2017-2-28
I'll assume that your table looks like this:
>> t = table({'No'; 10; 20}, {'No'; 10; 60}, {'No'; 'No'; 'No'}, 'VariableNames',{'Position' 'Velocity' 'Acceleration'})
t =
Position Velocity Acceleration
________ ________ ____________
'No' 'No' 'No'
[10] [10] 'No'
[20] [60] 'No'
(which is not at all clear). How would you do this with any MATLAB array? The answer is essentially the same: loop over the variables in the table, deleting the ones that are all No's, and loop backwards so you're not deleting things out from under yourself. "dot-parens-index" dubscripting makes this almost identical to what you'd do for a double array.
for j = width(t):-1:1
if all(strcmp(t.(j),'No'))
t.(j) = [];
end
end
Another way is to use varfun to figure out what to delete, and then delete all at once.
toDeleteFun = @(x) all(strcmp(x,'No'));
toDelete = varfun(toDeleteFun,t,'OutputFormat','uniform')
t(:,toDelete) = [];
The terminology for tables is "variables", not "fields" or "columns".
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!