Acquiring corresponding Columns of Data when a condition is met

11 views (last 30 days)
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

Accepted Answer

dpb
dpb on 14 Jun 2020
Edited: dpb on 15 Jun 2020
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 Comments
Sai Gudlur
Sai Gudlur on 14 Jun 2020
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!

Sign in to comment.

More Answers (1)

Rub Ron
Rub Ron on 14 Jun 2020
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 Comments
Sai Gudlur
Sai Gudlur on 14 Jun 2020
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!

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!