Find a value with cell array.

조회 수: 6 (최근 30일)
Robert
Robert 2015년 2월 24일
댓글: Robert 2015년 2월 25일
I'm using Matlab 7.14. I have a cell array with one column of numbers and one column of characters. i.e.
6 'text6'
5 'text5'
7 'text7'
3 'text3'
I want find a particular value (let's say 7) in the first column, and then use the reference to get at the text value in the same row (much like VLOOKUP in Excel). If this were just a numeric array I would use a = find(arrayname(:,1)==7) to get the row number (a would = 3) and then I could just get the text value via b = arrayname(a,2);
My question is how to find this reference/row number in my cell array? I've tried using a=cellfun(find(chart_list{:,1}==7)) but it returns the error 'Too many input arguments'. Can you use 'find' with cell arrays?
Any ideas how to find a value in an array?
Thanks Rob

채택된 답변

Jos (10584)
Jos (10584) 2015년 2월 24일
No need for cell2mat, simple concatenation would do:
val = [A{:,1}]
tf = val == 7
out = A(tf,2)
which you can do as a one-liner as well, of course:
A([A{:,1}]==7,2)
  댓글 수: 1
Robert
Robert 2015년 2월 25일
That works, thanks.

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

추가 답변 (1개)

Shoaibur Rahman
Shoaibur Rahman 2015년 2월 24일
편집: Shoaibur Rahman 2015년 2월 24일
A = your cell...
out = A(cell2mat(A(:,1))==7,2)
  댓글 수: 1
Robert
Robert 2015년 2월 25일
I get error 'Undefined function 'eq' for input arguments of type 'cell'.' if I use the code suggested. If I change the A(:,1) to A{:,1} I get the error ''== too many input arguments'. Jos's suggestion below seems to work though.
Thanks Rob

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

카테고리

Help CenterFile Exchange에서 Language Support에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by