필터 지우기
필터 지우기

How to extract certain rows from an excel sheet based on two categorical value columns?

조회 수: 15 (최근 30일)
Hello everyone!
I'm facing a problem by trying to extract just certain rows from an excel sheet using readtable, which I couldn't because of the way of the criterion. Hope I can find a solution here. Thanks in adavance.
The excel sheet has 9 columns with a multitude of rows (see attached file). The first column contains categorical values which are all numbers (many values of 1, many values of 2, and so on). The fifth column also contains categorical values such as 'Left', 'Right', 'Down', and 'Up', but also empty cells.
Suppose that for a row R1, the value in the first column is 1 and the fifth column is empty, while for a row R2 the value in the first column is also 1, but the fifth column contains the value "Right".
If in a row R the fifth column value is "Right", I want to extract that row as well as all other rows that the first column value is same with the first column value in row R, and write them in a new sheet.
I used the following, while I properly knew it wouldn't achieve all what I want:
T = readtable('196.xlsx','Sheet','196');
T(1:5,1:9);
data = T(:,{'id','name','frame','speed','turn','acceleration','xdistance','ydistance','time'});
data.turn = categorical(data.turn);
data0=data(data.turn=='Right',:);
filename = '196.xlsx';
writetable(data0,filename,'Sheet',2);
This wrote table with only rows with "Right" in the fith column, but instead I also need other rows that the fifth column is empty or has another value, but that the first column value is same with the first column value of a row that the fifth column value is "Right".
I'll apreciate your effort, if you could help me complete this code. Thanks!

채택된 답변

Voss
Voss 2022년 4월 9일
You can try this:
T = readtable('196.xlsx','Sheet','196');
T(1:5,1:9);
data = T(:,{'id','name','frame','speed','turn','acceleration','xdistance','ydistance','time'});
data.turn = categorical(data.turn);
% data0=data(data.turn=='Right',:);
data0 = data(ismember(data{:,1},data{data.turn=='Right',1}),:);
filename = '196.xlsx';
writetable(data0,filename,'Sheet',2);
An example to demonstrate how it works:
data = table([1 2 3 2 1].',{'Right' 'Right' 'Left' '' 'Right'}.','VariableNames',{'id','turn'})
data = 5×2 table
id turn __ __________ 1 {'Right' } 2 {'Right' } 3 {'Left' } 2 {0×0 char} 1 {'Right' }
data.turn = categorical(data.turn);
data0 = data(ismember(data{:,1},data{data.turn=='Right',1}),:)
data0 = 4×2 table
id turn __ ___________ 1 Right 2 Right 2 <undefined> 1 Right
  댓글 수: 8
ADJE JEREMIE ALAGBE
ADJE JEREMIE ALAGBE 2022년 4월 11일
Thank you so much for you kind help! I believe your code might be correct and maybe my data has a problem. Actually, based on the original meaning of the data, the fifth column of the resulting sheet should include only RIGHT turns and empty cells.
I will check if there is something wrong with the data.
But I'm going to accept the answer.
Thanks again!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by