Acquiring corresponding Columns of Data when a condition is met
4 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a question have an excel Sheet with mulitple columns of data and No.of elements/rows in each column are same. One of the column is Status, Under this column options are "Accept" or "Reject". When i run my code it perfectly recognizes all the "accepts" in the column but when based on the column i intend to acquire/extract corresponding elements data or other column data which line up with "Accept" i am unable to extract. Also attaching sample of my excel file.
Thanks
Sai
A = importdata('Sample.xlsx');
headers = A.textdata;
prog = A.data(:,1);
Status = A.textdata(2:end,3); % Since the headers was also in textdata i indexed from 2nd Row.
prog_2 = [];
for i = 1:size(Status)
if contains(Status,"Accept")
prog_2 = prog(i);
end
end
0 个评论
采纳的回答
dpb
2020-6-14
编辑:dpb
2020-6-15
Recommend readtable for disparate data -- much easier to handle variables of different types...
tSample=readtable('Sample.xlsx'); % read the table
tSample.Status=categorical(tSample.Status); % turn Status into categorical type (could do on read)
All it takes to get a neat table -- to get the given entries is also simple--
tOK=tSample(tSample.Status=='Accept',:); % return all columns for rows Status is "Accept"
% returns
>> tOK
tOK =
15×3 table
ABC XYZ Status
_______ ____ ______
1020.00 1.25 Accept
1020.00 1.26 Accept
1020.00 1.28 Accept
1024.00 1.29 Accept
1026.00 1.32 Accept
1026.00 1.33 Accept
1026.00 1.34 Accept
1026.00 1.35 Accept
1026.00 1.36 Accept
1026.00 1.37 Accept
1032.00 1.38 Accept
1032.00 1.39 Accept
1032.00 1.40 Accept
1032.00 1.42 Accept
1032.00 1.46 Accept
>>
There are all kinds of other goodies available as well -- groupsummary being one along w/ groupstats
>> groupsummary(tSample,"Status")
ans =
2×2 table
Status GroupCount
______ __________
Accept 15.00
Reject 5.00
>>
Don't get much better than this!!! :)
3 个评论
更多回答(1 个)
Rub Ron
2020-6-14
If I understood correctly, you are trying to obtain the values of the first column which has 'accepted' status. If so, try this:
A = readtable('Sample.xlsx');
desired_data = A.ABC(strcmp(A.Status,'Accept'));
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!