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일
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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by