Hello,
I have a table and would like to get out the specific lines based on chosen strings of the first and second column. Means when you look at the attached picture, I would like to have e.g. all the lines with 'Fossil Gas' and 'NO_1' and maybe build them up in a new table or better just take out the values of the last column.
Thank you for the answer! :)

 채택된 답변

jonas
jonas 2018년 10월 23일
편집: jonas 2018년 10월 23일

0 개 추천

Just use logical indexing
T{strcmp(T{:,1},'Fossil Gas'),end}
The line
strcmp(T{:,1},'Fossil Gas')
returns a logical vector where rows matching the target string are true. This vector is then used to extract values from the last (end) column.
Depending on what you are trying to do, it may be wise to use the first column as a grouping variable, and evaluate the other columns group-wise.

댓글 수: 4

lakehead
lakehead 2018년 10월 23일
Thank you! But is it possible to return a logical vector for the first and second column so I get only true e.g. for 'Fossil Gas' and 'R_NO1' ? Because like this I have the values of all 'Fossil Gas'
jonas
jonas 2018년 10월 23일
편집: jonas 2018년 10월 23일
You can add as many conditions as you'd like
T{strcmp(T{:,1},'Fossil Gas') | strcmp(T{:,2},'R_NO1'),end}
The "|" means OR, so it returns true if one or both conditions are satisfied. Perhaps you only want those rows where both conditions are satisified? If so, change to "&"
lakehead
lakehead 2018년 10월 23일
Thank you perfect and for the tip with the grouping, too!!! I think it might be better.
jonas
jonas 2018년 10월 23일
편집: jonas 2018년 10월 23일
My pleasure! Your data set indeed seems ideal for grouping. To get you started, you can look into the following functions
  • findgroups & splitapply
  • varfun
  • grpstats
They are three different functions for evaluating grouped data. If I had to pick one... I would probably go for varfun.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

태그

질문:

2018년 10월 23일

편집:

2018년 10월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by