Find specific entry based on column value in table

Apologies if this has been asnwered somewhere but I was unable to find a simple answer to what I strongly suspect is a really simple operation in MATLAB. I have a simple table (see attached), and I want to extract the number that corresponds to the name 'Bob'. (so in this case, 2). How would I extract this? Essentially I am looking for the row index of a table where the column value is something specific.
Thanks,

 채택된 답변

Cris LaPierre
Cris LaPierre 2021년 4월 30일
I'd create a logical array of the name column, and use that to extract the number from the first column. You can learn about logical arrays in Ch 12 of MATLAB Onramp. This page on accessing data in a table is likely also helpful.
The code might be something like this
n = data.Number(data.Name=='Bob')

댓글 수: 4

NFC
NFC 2021년 4월 30일
편집: NFC 2021년 4월 30일
Thanks for the response, I have tried this already but I kept getting the error that "operator == is not supported for operants of type 'cell'"
Maybe this is because of the way that I specified the variables? Every entry in 'name' was specified as a cell (this is not something I can change as it is dependent on some external software.
Specifically, I am using a table extracted from a dicom file using the .ROIs method as shown in this example https://uk.mathworks.com/help/images/ref/dicomcontours.createmask.html
For this specific type of table where the entries of Name are cells,
data.Number(strcmp(data.Name, 'Bob'))
should do the trick
How are you loading your table? Consider using readtable.
NFC
NFC 2021년 4월 30일
편집: NFC 2021년 4월 30일
I am actually not doing the loading myself, I'm reading it off a dicomfile as shown on the mathworks website
dicominfo(path)
and then accessing contours with
contours=dicomContours(info);
and finally loading the table with
ROIs=contours.ROIs;
I missed that detail. I think I like the strcmp approach better anyway.

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

추가 답변 (0개)

카테고리

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

태그

질문:

NFC
2021년 4월 30일

댓글:

2021년 4월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by