필터 지우기
필터 지우기

Indexing into a cell array to retrieve specific value

조회 수: 1 (최근 30일)
Shannon McNee
Shannon McNee 2017년 7월 11일
댓글: Shannon McNee 2017년 7월 11일

Hello,

I have a 8x5 cell array called dat_cell, like this:

dat = {...
9059	24	'x'	'F'	'United Kingdom'
10764	32	'x'	'F'	'United Kingdom'
11138	22	'x'	'M'	'United Kingdom'
11334	24	'x'	'M'	'United Kingdom'
11965	24	'x'	'M'	'United Kingdom'
12095	29	'x'	'F'	'United Kingdom'
12270	23	'x'	'F'	'United Kingdom'
12547	21	'x'	'F'	'United Kingdom'}

Where the first column is ID number, second is age, third is email address, fourth is gender and fifth is location.

I would like to be able to index into this cell array using a specific subjects email address as a string to retrieve their specific ID number.

For example, if I created a string like:

sj_name = 'xxxx@gmail.com' 

Would I then be able to use this string to index into dat_cell and find the ID number associated with that email?

I'm sure this is really simple and I'm just over complicating things. I have been trying to use functions such as find but haven't had any luck.

Thank you in advance for any help.

채택된 답변

Guillaume
Guillaume 2017년 7월 11일
You would probably be better off using a table instead of a cell array.
In any case:
sj_name = 'xxxx@gmail.com';
ids = cell2mat(yourcellarray(strcmp(yourcellarray(:, 3), sj_name), 1));
If you are absolutely certain that there is only ever one match:
id = yourcellarray{strcmp(yourcellarray(:, 3), sj_name), 1};
  댓글 수: 1
Shannon McNee
Shannon McNee 2017년 7월 11일
Thank you, both options were extremely helpful and gave me exactly what I need.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2017년 7월 11일
Use ismember().

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by