Acquiring corresponding Columns of Data when a condition is met

22 ビュー (過去 30 日間)
Sai Gudlur
Sai Gudlur 2020 年 6 月 14 日
コメント済み: dpb 2020 年 6 月 15 日
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

採用された回答

dpb
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 件のコメント
Sai Gudlur
Sai Gudlur 2020 年 6 月 14 日
Just another question.
What if i want to incorporate a listdlg where i/user is asked to make one or multiple selection from the Column "ABC". Once the selection is made i would want only the selected ones in a form of a table.
My earlier question was answered by both you and Rub Ron but this is pretty much like putting a filter on each of the fields. So to make it more robust i was hoping i could add a listdlg. Do not intend uiinput as list might be easier displaying options and helps me choose "Single" or "Multi".
Thanks!
dpb
dpb 2020 年 6 月 15 日
Sure. Just use unique(tSample.ABC) as the list.

サインインしてコメントする。

その他の回答 (1 件)

Rub Ron
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'));
  2 件のコメント
Sai Gudlur
Sai Gudlur 2020 年 6 月 14 日
Thank you Rub Ron!
Sai Gudlur
Sai Gudlur 2020 年 6 月 14 日
Just another question.
What if i want to incorporate a listdlg where i/user is asked to make one or multiple selection from the Column "ABC". Once the selection is made i would want only the selected ones in a form of a table.
My earlier question was answered by both you and Rub Ron but this is pretty much like putting a filter on each of the fields. So to make it more robust i was hoping i could add a listdlg. Do not intend uiinput as list might be easier displaying options and helps me choose "Single" or "Multi".
Thanks!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by