tables remove one field

조회 수: 1 (최근 30일)
Patrick Brown
Patrick Brown 2017년 2월 23일
답변: Peter Perkins 2017년 2월 28일
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

답변 (3개)

Chibuzo Nnonyelu
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
  댓글 수: 1
Jan
Jan 2017년 2월 23일
편집: Jan 2017년 2월 23일
Or:
if all(strcmp('No', table2cell(T(:, m))))
I cannot try this currently, but would this work also:
if all(strcmp('No', T{:, m}))

댓글을 달려면 로그인하십시오.


Patrick Brown
Patrick Brown 2017년 2월 23일
It does not work well because you have this error Variable index exceeds table dimensions.
maybe because you are changing the width of T when you delete columns

Peter Perkins
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".

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by