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!